【问题标题】:preg_replace PHP not working?preg_replace PHP 不工作?
【发布时间】:2012-12-09 06:47:31
【问题描述】:

为什么 preg_replace 在这种情况下不返回任何东西?我整晚都在想办法。

这是 $postContent 中包含的文本:

测试一下。这是一个报价:[Quote]1[/Quote] 报价现已结束。

这是我的代码:

echo "Test I'm Here!!!";
                    $startQuotePos = strpos($postContent,'[Quote]')+7;
                    $endQuotePos = strpos($postContent,'[/Quote]');
                    $postStrLength = strlen($postContent);
                    $quotePostID = substr($postContent,$startQuotePos,($endQuotePos-$postStrLength));
                    $quotePattern = '[Quote]'.$quotePostID.'[/Quote]';
                    $newPCAQ = preg_replace($quotePattern,$quotePostID,$postContent);
                    echo "<br />$startQuotePos<br />$endQuotePos<br />$quotePostID<br />Qpattern:$quotePattern<br />PCAQ: $newPCAQ<br />";

这是我的结果:

测试我在这里!!!

35

36

1

Qpattern:[Quote]1[/Quote]

PCAQ:

【问题讨论】:

    标签: php preg-replace


    【解决方案1】:

    对于preg_replace()"[Quote]" 匹配以下之一的单个字符:quo、t,或 e.

    如果您希望 preg_replace() 找到文字 "[Quote]",则需要将其转义为 "\[Quote\]"preg_quote() 是你应该使用的函数:preg_quote("[Quote]")

    您的代码也是错误的,因为正则表达式应该以分隔符开头。在preg_replace() 电话中,我在答案末尾显示,即@,但您可以使用另一个字符,只要它不出现在正则表达式中,并且它是也用于正则表达式的末尾。 (在我的例子中,@ 后跟一个模式修饰符,而模式修饰符是模式分隔符后唯一允许的字符。)

    如果您要使用preg_replace(),那么首先找到"[Quote]" 的位置是没有意义的。我宁愿使用以下代码:

    $newPCAQ = preg_replace('@\[Quote\](.+?)\[/Quote\]@i', '\1', $postContent);
    

    我将解释我正在使用的正则表达式:

    • 最后的'@i' 告诉preg_replace() 忽略小写和大写字符之间的区别;该字符串可以包含"[QuOte]234[/QuOTE]",并且该子字符串将与正则表达式匹配。

    • 我在"(.+?)" 中使用问号,以避免".+" 过于贪婪,匹配太多字符。没有它,正则表达式可以在单个匹配中包含一个子字符串,如"[Quote]234[/Quote] Other text [Quote]475[/Quote]",而这应该作为两个子字符串匹配:"[Quote]234[/Quote]""[Quote]475[/Quote]"

    • 我用作替换字符串的'\1' 字符串是告诉preg_replace() 使用从子组"(.+?)" 匹配的字符串作为替换。换句话说,对preg_replace() 的调用正在删除"[Quote]""[/Quote]" 周围的其他文本。 (它不会替换与"[Quote]" 不匹配的"[/Quote]",例如"[/Quote] Other text [Quote]"。)

    【讨论】:

    • 感谢您的解释。
    【解决方案2】:

    您的正则表达式必须以“/”开头和结尾:

    $quotePattern = '/[Quote]'.$quotePostID.'[/Quote]/';
    

    【讨论】:

    • 谢谢。我想我真的需要努力理解正则表达式。现在 str_ireplace 为我工作。其中任何一个(str_ireplace vs. preg_replace)是否比另一个更有效?
    • 我做了一些研究,看起来 str_replace 对于像你这样的简单字符串替换稍微快一些。但我怀疑这有什么可担心的,因为差异是如此微不足道。
    【解决方案3】:

    preg_replace 的返回值看不到任何内容的原因是它返回了NULL有关详细信息,请参阅手册链接)。这是发生错误时 preg_replace 返回的内容,这就是您的情况。 NULL 的字符串值是一个零长度字符串。您可以通过使用 var_dump 来查看这一点,它会告诉您 preg_replace 返回 NULL。

    您的正则表达式无效,因此 PHP 将抛出 E_WARNING 级别的错误 Warning: preg_replace(): Unknown modifier '['

    这有几个原因。首先,您需要为正则表达式指定一个开始和结束分隔符,因为 preg_* 函数使用 PCRE style 正则表达式。其次,您还想考虑在您的模式上使用preg_quote没有分隔符)以确保它被正确转义。

    $postContent = "Test this. Here is a quote: [Quote]1[/Quote] Quote is now over.";
    /* Specify a delimiter for your regular expression */
    $delimiter = '@';
    
    $startQuotePos = strpos($postContent,'[Quote]')+7;
    $endQuotePos = strpos($postContent,'[/Quote]');
    $postStrLength = strlen($postContent);
    $quotePostID = substr($postContent,$startQuotePos,($endQuotePos-$postStrLength));
    /* Make sure you use the delimiter in your pattern and escape it properly */
    $quotePattern = $delimiter . preg_quote("[Quote]{$quotePostID}[/Quote]", $delimiter) . $delimiter;
    $newPCAQ = preg_replace($quotePattern,$quotePostID,$postContent);
    echo "<br />$startQuotePos<br />$endQuotePos<br />$quotePostID<br />Qpattern:$quotePattern<br />PCAQ: $newPCAQ<br />";
    

    输出将是:

    35

    36

    1

    Qpattern:@[Quote]1[/Quote]@

    PCAQ:测试一下。这是一个报价:1 报价现已结束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多