【发布时间】:2015-05-09 12:40:00
【问题描述】:
感谢this post 中的信息,我已经成功地将我需要更新的大部分 preg_replace 语句转换为 preg_replace_callback 语句。
但是,当我转换以下语句时:
$body_highlighted = preg_replace('/((<[^>]*)|' . preg_quote(strtr($query, array('\'' => ''')), '/') . ')/ie' . ($context['utf8'] ? 'u' : ''),
"'\$2' == '\$1' ? stripslashes('\$1') : '<strong class=\"highlight\">\$1</strong>'",
$body_highlighted);
到
$body_highlighted = preg_replace_callback('/((<[^>]*)|' . preg_quote(strtr($query, array('\'' => ''')), '/') . ')/i' . ($context['utf8'] ? 'u' : ''),
function ($matches) {
return $matches[2] == $matches[1] ? stripslashes($matches[1]) : "<strong class=highlight>$matches[1]</strong>";
},
$body_highlighted);
出现错误信息'Undefined offset: 2'(原来的preg_replace语句不会产生这个错误)。
我花了几个小时试图解决这个问题,但由于我以前从未做过 PHP 编程,我真的不知道为什么它不起作用或如何解决它。
【问题讨论】:
标签: php preg-replace preg-replace-callback