将preg_replace转换为preg_replace_callback后的“未定义的偏移量”

时间:2021-06-26 16:54:20

Thanks to the information in this post I've successfully managed to convert most of the preg_replace statements I needed to update to preg_replace_callback statements.

感谢这篇文章中的信息,我成功地将大多数需要更新的preg_replace语句转换为preg_replace_callback语句。

However, when I convert the following statement:

但是,当我转换以下语句时:

$body_highlighted = preg_replace('/((<[^>]*)|' . preg_quote(strtr($query, array('\'' => '&#039;')), '/') . ')/ie' . ($context['utf8'] ? 'u' : ''), 
    "'\$2' == '\$1' ? stripslashes('\$1') : '<strong class=\"highlight\">\$1</strong>'", 
        $body_highlighted);

to

$body_highlighted = preg_replace_callback('/((<[^>]*)|' . preg_quote(strtr($query, array('\'' => '&#039;')), '/') . ')/i' . ($context['utf8'] ? 'u' : ''), 
    function ($matches) {
        return $matches[2] == $matches[1] ? stripslashes($matches[1]) : "<strong class=highlight>$matches[1]</strong>";
    }, 
      $body_highlighted);

the error message 'Undefined offset: 2' occurs (the original preg_replace statement does not generate this error).

出现错误消息'Undefined offset:2'(原始preg_replace语句不会生成此错误)。

I've spent several hours trying to fix this issue but, as I've never done PHP programming before, I really don't know why it isn't working or how to fix it.

我花了几个小时试图解决这个问题但是,因为我以前从未做过PHP编程,所以我真的不知道它为什么不工作或如何解决它。

1 个解决方案

#1


Your pattern contains an alternation. In the first branch of this alternation, the group 2 is defined, but it isn't true in the second branch. So if the second branch succeeds the capture group 2 is not defined (as $matches[2])

您的模式包含一个替代。在该交替的第一个分支中,定义了组2,但在第二个分支中不是这样。因此,如果第二个分支成功,则未定义捕获组2(如$ matches [2])

To solve the problem you only need to test if $matches[2] exists with isset()

要解决这个问题,你只需要测试isset()是否存在$ matches [2]

But you can write this in a more simple way if you remove the useless capture group that enclosed all the pattern:

但是如果删除包含所有模式的无用捕获组,您可以用更简单的方式编写它:

$pattern = '/(<[^>]*)|' . preg_quote(str_replace("'", '&#039;', $query), '/')
         . '/i' . ($context['utf8'] ? 'u' : '');

$body_highlighted = preg_replace_callback($pattern, function ($m) {
    return isset($m[1]) ? stripslashes($m[0])
                        : '<strong class="highlight">' . $m[0] . '</strong>';
}, $body_highlighted);

#1


Your pattern contains an alternation. In the first branch of this alternation, the group 2 is defined, but it isn't true in the second branch. So if the second branch succeeds the capture group 2 is not defined (as $matches[2])

您的模式包含一个替代。在该交替的第一个分支中,定义了组2,但在第二个分支中不是这样。因此,如果第二个分支成功,则未定义捕获组2(如$ matches [2])

To solve the problem you only need to test if $matches[2] exists with isset()

要解决这个问题,你只需要测试isset()是否存在$ matches [2]

But you can write this in a more simple way if you remove the useless capture group that enclosed all the pattern:

但是如果删除包含所有模式的无用捕获组,您可以用更简单的方式编写它:

$pattern = '/(<[^>]*)|' . preg_quote(str_replace("'", '&#039;', $query), '/')
         . '/i' . ($context['utf8'] ? 'u' : '');

$body_highlighted = preg_replace_callback($pattern, function ($m) {
    return isset($m[1]) ? stripslashes($m[0])
                        : '<strong class="highlight">' . $m[0] . '</strong>';
}, $body_highlighted);