Search code examples
phppreg-replacepreg-replace-callback

'Undefined offset' after converting preg_replace to preg_replace_callback


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.

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).

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.


Solution

  • 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])

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

    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);