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 Link to comment https://forums.phpfreaks.com/topic/268974-removing-currency-symbol/ Share on other sites More sharing options...
Jessica Posted October 1, 2012 Share Posted October 1, 2012 floatval Better yet, find at what point the $ is added, and remove it there. Link to comment https://forums.phpfreaks.com/topic/268974-removing-currency-symbol/#findComment-1382106 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. Link to comment https://forums.phpfreaks.com/topic/268974-removing-currency-symbol/#findComment-1382110 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; ?> Link to comment https://forums.phpfreaks.com/topic/268974-removing-currency-symbol/#findComment-1382114 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" Link to comment https://forums.phpfreaks.com/topic/268974-removing-currency-symbol/#findComment-1382116 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. Link to comment https://forums.phpfreaks.com/topic/268974-removing-currency-symbol/#findComment-1382121 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. Link to comment https://forums.phpfreaks.com/topic/268974-removing-currency-symbol/#findComment-1382123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.