new-2-php Posted October 1, 2012 Share Posted October 1, 2012 Hi Guys, As my username suggests I'm new to PHP and I work largely with client side code. I'm in need of your help and hope someone here can kindly provide a solution or point me in the right direction. I'm working with a serverside script and there is the following line of code: <?php echo $order_total ?> When this renders it displays an amount in addition to the currency symbol (i.e. $10) and I need to remove the currency symbol so it only displays the amount. Is it possible to do this through PHP and if so how? I have read articles on ways to potentially achieve (i.e. preg_replace) this but had no luck in implementing them - hence im here! Your assistance and advice would be greatly appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 1, 2012 Share Posted October 1, 2012 (edited) floatval Better yet, find at what point the $ is added, and remove it there. Edited October 1, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
new-2-php Posted October 1, 2012 Author Share Posted October 1, 2012 Thanks for the reply Jessica. That would be ideal but I need it to display the symbol on the wider site whilst not displaying it in this one area. Quote Link to comment Share on other sites More sharing options...
White_Lily Posted October 1, 2012 Share Posted October 1, 2012 You could try str_replace() e.g: <?php $order_total = "$10"; $order_total = str_replace("$", "", $order_total); echo $order_total; ?> Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 1, 2012 Share Posted October 1, 2012 That would be ideal but I need it to display the symbol on the wider site whilst not displaying it in this one area. You should always be adding things like formatting and currency symbols at the very instant you display them. Otherwise, you run into problems like this. Are you remembering that there may be a comma in these numbers if you format them using number_format? Do you know: $a = "123,456.78"; echo $a * 2;//echoes "246" Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 1, 2012 Share Posted October 1, 2012 You should always be adding things like formatting and currency symbols at the very instant you display them. Otherwise, you run into problems like this. Are you remembering that there may be a comma in these numbers if you format them using number_format? Do you know: $a = "123,456.78"; echo $a * 2;//echoes "246" Exactly - remove it from the original place, and only output it when displaying. If you don't like that you can assign the formatted one to a new var, and use that as your display, then have the original value still intact. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 1, 2012 Share Posted October 1, 2012 This is the basis of the MVC coding style. You're mixing display logic (adding formatting and currency symbols) in with controller-level logic. If you ever do anything with these prices other than printing them (like adding them or sorting them) you will get the wrong result and not understand why. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.