I have a script that I want to modify by taking the first letter out of it, adding a figure to the remainder and then adding the first letter back in. This probably sounds like double dutch so here it is in more understandable english: I want to remove the first character out of this string $string . This string varies in value but the first character is always £ . I want to remove the £ and leave the remaining value e.g. 20.00 or 1000.00. I then want to add a figure to it. e.g. say 10.00 add 1.50 to equal 11.50 I then want to add the £ back in to finish up with £11.50 Any help would be appreciated. John C
Try something like this... $string = '$20.00'; $chop = intval(substr($string, 1)); // $chop now is 20.00 $add = 10; $total = $chop + $add; // total now is 30 $finalValue = '$' .$total; // should be $30.00
Hi Guys... Many thanks for your help. However, I have played about with this code, using both floatval() and intval(), but the final result drops the final 0's I have also tried strval() with the same results. i.e. If the final value of $chop + $add; is £30.00 it is displayed as £30 (without the final 2 00's) If the final value of $chop + $add; is £30.50 it is displayed as £30.5 (without the final 0) If the final value of $chop + $add; is £30.95 it is displayed as £30.95 (which is what I want) This project is to add into a shopping cart that has no facility for adding carriage to the final total so it is important that I get the final result to 2 decimal places. Any other thoughts will be appreciated. Regards, John C
string money_format ( string $format , float $number ) http://php.net/manual/en/function.money-format.php with this function you can specify the number of decimal places to display along with other stuff
Hi shofstetter.. Thanks for your help. I couldn't get money_format() to work as I needed it. However, whilst trying I came across number_format() which worked a trreat for me. Thanks for your help. John C
You can also do: $amount = '£20.00'; echo str_replace('£', '', $amount); // or echo str_replace('£', '', $amount); // if reading string from a url PHP: