PHP条带标点符号保持撇号

时间:2012-05-23 17:26:10

标签: php regex

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

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

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:2)

以下是第一个例子的改编:

$string = preg_replace('/[^a-z0-9\' ]+/i', '', $string);
相关问题