Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot work out a php str_replace() to remove comma

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.

like image 883
KiloJKilo Avatar asked Jul 17 '12 15:07

KiloJKilo


People also ask

How to remove commas in PHP?

To remove comma, you can replace. To replace, use str_replace() in PHP.

How do I remove all commas from a string?

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.

How do you remove the last comma?

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!


1 Answers

$string = str_replace(',', '', $string);
like image 93
Yan Berk Avatar answered Nov 14 '22 21:11

Yan Berk