【问题标题】:PHP strip punctuation keep apostrophePHP带标点符号保持撇号
【发布时间】:2012-05-23 17:26:10
【问题描述】:

喜欢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

如何使用正则表达式或其他方式做到这一点?

【问题讨论】:

  • 我试过 $string = preg_replace('/[^a-z0-9\']/', '', $string);好像没什么用。我的字符串来自一个 html 页面。撇号是 ’例如,Don't = Don’t

标签: php regex


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      相关资源
      最近更新 更多