In a PHP project, I have:
$string = "1,555";
str_replace(',', '', $string);
echo $string; //is still 1,555
str_replace does not remove the comma. If I var_dump. I get string(5) "1,555"
Any ideas? I just simply need to remove the commas so I can compute.
To remove comma, you can replace. To replace, use str_replace() in PHP.
To remove all commas from a string, call the replace() method, passing it a regular expression to match all commas as the first parameter and an empty string as the second parameter. The replace method will return a new string with all of the commas removed.
To remove the last comma from a string, call the replace() method with the following regular expression /,*$/ as the first parameter and an empty string as the second. The replace method will return a new string with the last comma removed. Copied!
$string = str_replace(',', '', $string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With