2012-05-23 36 views
3

PHP strip punctuation,但我想保留撇号。 例子:PHP地带标点符号保留撇号

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

条后,该字符串应该是:

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

我怎么能做到这一点与正则表达式或其他方式?

+0

我试过$ string = preg_replace('/ [^ a-z0-9 \'] /','',$ string);这似乎没有用。我的字符串来自html页面。撇号是’例如,Do not = Don ’ t – droughtrain

回答

3

如果要支持所有Unicode标点字符以及然后使用这个表达式:

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

这正则表达式是匹配的Unicode标点字符类\pP并且匹配将使用负向预测来避免撇号字符。

+0

我刚刚学到了一些东西 –

+0

这很棒。非常酷。 – droughtrain

2

这里的第一个例子的改编:

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