【问题标题】:punctuation ereg_replace preg_replace标点符号 ereg_replace preg_replace
【发布时间】:2011-08-05 22:30:29
【问题描述】:

我有这个来自旧 OsCommerce 安装的 sn-p 代码

    $pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                    ?   "([^[:alnum:]])+"
                    :   "([[:punct:]])+";

我想修改 [:punct:] 选择器,使其不包括 - 破折号。

下一行代码是

$anchor = ereg_replace($pattern, '', strtolower($string));

删除以前找到的字符。我怎样才能保留我的破折号?

谢谢,马里奥

编辑

我想我明白了:

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                    ?   "([^[:alnum:]])+"
                    :   "([^-a-zA-Z0-9[:space:]])+";

注意:破折号必须先出现。或者,对于下划线:

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                    ?   "([^[:alnum:]])+"
                    :   "([^a-zA-Z0-9_[:space:]])+";

我不知道如何使用负前瞻 :(。 干杯。马里奥

【问题讨论】:

    标签: php regex replace punctuation ereg


    【解决方案1】:

    您可能需要创建自己的[characterset] 而不是使用[:punct:]

    这个看起来不错,但您需要验证它。

    [^a-zA-Z0-9-\s]
    

    这将替换任何不是 (a-z) 字母、数字、空格或破折号的内容。

    $pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                ?   "([^[:alnum:]])+"
                :   "[^a-zA-Z0-9-\s]+";
    

    编辑:旧答案行不通,因为ereg doesn't support lookaround

    试试这个否定的前瞻(?!-)

    $pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true'
                    ?   "([^[:alnum:]])+"
                    :   "((?!-)[[:punct:]])+";
    

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 1970-01-01
      • 2012-10-01
      • 2012-07-20
      • 2012-03-09
      • 2012-01-28
      • 2013-08-06
      • 1970-01-01
      相关资源
      最近更新 更多