用逗号替换逗号后跟空格 [英] 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 (" "
).
推荐答案
所有str_replace解决方案都可以使用.如果要替换逗号前后的所有空格
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);
这篇关于用逗号替换逗号后跟空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文