如何从字符串中去除逗号和句点?
问题描述:
我已经包含字符串是这样的:如何从字符串中去除逗号和句点?
"Favourite bands: coldplay, guns & roses, etc.,"
如何使用preg_replace
我删除逗号和句号?
答
你可以使用
preg_replace('/[.,]/', '', $string);
但使用简单的字符替换正则表达式是矫枉过正。
你会更好使用strtr:
strtr($string, array('.' => '', ',' => ''));
str_replace(array('.', ','), '' , $string);