Php 将逗号后跟空格替换为逗号

Php 将逗号后跟空格替换为逗号,php,replace,preg-replace,Php,Replace,Preg Replace,如何将所有逗号后跟空格(“,”)替换为逗号(“,”) 我不想在空格前面没有逗号时替换空格(“”)。这样可以吗 $sString = str_replace(", ", ",", $sString); 你可以做: $str = str_replace(', ',',',$str); 这应该可以做到: $str = "some, comma, seperated, words"; $str = str_replace(", ", ",", $str); 所有stru_替换解决方案都会起作用。如

如何将所有逗号后跟空格(
“,”
)替换为逗号(
“,”


我不想在空格前面没有逗号时替换空格(
”)。

这样可以吗

$sString = str_replace(", ", ",", $sString);
你可以做:

$str = str_replace(', ',',',$str);

这应该可以做到:

$str = "some, comma, seperated, words";
$str = str_replace(", ", ",", $str);

所有stru_替换解决方案都会起作用。如果要替换逗号前后的所有空格

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

$pattern = '/\s*,\s*/';
$replace = ',';
$str = preg_replace($pattern, $replace, $str);

这才是真正的答案!请给出8年后的代码示例?认真地