Replace commas followed by spaces with commas

后端 未结 4 1263
傲寒
傲寒 2020-12-16 03:04

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

相关标签:
4条回答
  • 2020-12-16 03:23

    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);
    
    0 讨论(0)
  • 2020-12-16 03:27

    This will do the trick?

    $sString = str_replace(", ", ",", $sString);
    
    0 讨论(0)
  • 2020-12-16 03:38

    This should do the trick:

    $str = "some, comma, seperated, words";
    $str = str_replace(", ", ",", $str);
    
    0 讨论(0)
  • 2020-12-16 03:43

    You can do:

    $str = str_replace(', ',',',$str);
    
    0 讨论(0)
提交回复
热议问题