如何从字符串中去掉逗号和句号?

34

我有一个包含以下内容的字符串:

"Favourite bands: coldplay, guns & roses, etc.,"

我该如何使用preg_replace去除逗号和句号?

1个回答

131
你可以使用 来实现。
preg_replace('/[.,]/', '', $string);

但是仅用正则表达式进行简单字符替换有些过头了。

更好的选择是使用strtr

strtr($string, array('.' => '', ',' => ''));

str_replace

str_replace(array('.', ','), '' , $string);

Unicode字符串怎么样? - Arnaud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接