3

PHP strip punctuation一样,但我想保留撇号。例子:

I'm a student, but I don't like the school. I'll quit school.

strip 之后的字符串应该是:

I'm a student but I don't like the school I'll quit school

我怎样才能用正则表达式或其他方式做到这一点?

4

2 回答 2

4

如果您还想支持所有 Unicode 标点符号,请使用此正则表达式:

$str = preg_replace("#((?!')\pP)+#", '', $str);

此正则表达式匹配 Unicode 标点字符类\pP,并且匹配将避免使用负前瞻的撇号字符。

于 2012-05-23T17:42:22.287 回答
2

这是对第一个示例的改编:

$string = preg_replace('/[^a-z0-9\' ]+/i', '', $string);
于 2012-05-23T17:30:12.733 回答