简体   繁体   English

PHP条带标点符号保持撇号

[英]PHP strip punctuation keep apostrophe

Like PHP strip punctuation , but I want to keep the apostrophe. PHP条带标点符号 ,但我想保持撇号。 Example: 例:

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

The string after strip should be: 条带后的字符串应为:

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

How can I do that with regular expression or other ways? 我怎么能用正则表达式或其他方式做到这一点?

If you want to support all Unicode punctuation characters as well then use this regex: 如果您还想支持所有Unicode标点字符,请使用此正则表达式:

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

This regex is matching Unicode punctuation character class \\pP and match will avoid apostrophe character using negative lookahead. 这个正则表达式匹配Unicode标点字符类\\pP ,匹配将避免使用否定前瞻的撇号字符。

这是第一个例子的改编:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM