这些preg_*
函数期望Perl兼容正则表达式和分隔符.试试这个:
preg_replace('/[[:punct:]]/', ' ', $string)
注意:g
PHP的PCRE
实现不需要修饰符!
除了Gumbo的答案,使用g
修饰符替换标点符号的所有出现:
preg_replace('/[[:punct:]]/g', ' ', $string) // ^
来自Johnathan Lonowski(见评论):
> [The g modifier] means "Global" -- i.e., find all existing matches. Without it, regex functions will stop searching after the first match.