Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace commas followed by spaces with commas

How can I replace all commas followed by spaces (", ") with just commas (",")?

I don't want to replace spaces when they don't have a comma in front of them (" ").

like image 992
Philip Avatar asked Jun 09 '11 08:06

Philip


3 Answers

All the str_replace solutions will work. If you want to replace all whitespaces before and after the commas

$str = 'cat,  dog , cow,       horse   ,mouse,moose';

$pattern = '/\s*,\s*/';
$replace = ',';
$str = preg_replace($pattern, $replace, $str);
like image 96
David Chan Avatar answered Oct 07 '22 04:10

David Chan


This should do the trick:

$str = "some, comma, seperated, words";
$str = str_replace(", ", ",", $str);
like image 26
Chris Laarman Avatar answered Oct 07 '22 04:10

Chris Laarman


This will do the trick?

$sString = str_replace(", ", ",", $sString);
like image 33
Wesley van Opdorp Avatar answered Oct 07 '22 06:10

Wesley van Opdorp