我曾经有一段代码来模拟一些BBCode:
$pattern = array(
'/\\n/',
'/\\r/',
'/\[list\](.*?)\[\/list\]/ise',
'/\[b\](.*?)\[\/b\]/is',
'/\[strong\](.*?)\[\/strong\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[s\](.*?)\[\/s\]/is',
'/\[del\](.*?)\[\/del\]/is',
'/\[url=(.*?)\](.*?)\[\/url\]/ise',
'/\[email=(.*?)\](.*?)\[\/email\]/is',
'/\[img](.*?)\[\/img\]/ise',
'/\[color=(.*?)\](.*?)\[\/color\]/is',
'/\[font=(.*?)\](.*?)\[\/font\]/ise',
'/\[bg=(.*?)\](.*?)\[\/bg\]/ise',
'/\[size=(.*?)\](.*?)\[\/size\]/ise'
);
$replace = array(
'<br/>',
'',
'$this->sList(\'\\1\')',
'<b>\1</b>',
'<strong>\1</strong>',
'<i>\1</i>',
'<span style="text-decoration: underline;">\1</span>',
'<span style="text-decoration: line-through;">\1</span>',
'<span style="text-decoration: line-through;">\1</span>',
'$this->urlfix(\'\\1\',\'\\2\')',
'<a href="mailto:\1" title="\1">\2</a>',
'$this->imagefix(\'\\1\')',
'<span style="color: \1;">\2</span>',
'$this->fontfix(\'\\1\',\'\\2\')',
'$this->bgfix(\'\\1\',\'\\2\')',
'$this->sizefix(\'\\1\',\'\\2\')'
);
return preg_replace($pattern, $replace, nl2br(stripslashes($string)));
但是我正在升级到 PHP 5.5,这里出现了错误,以前它工作得很好,这是我收到的错误信息:
已弃用:preg_replace():/e修饰符已弃用,请改用 preg_replace_callback in
我尝试了几种方法,但目前还没有任何进展。
这是我迄今为止尝试的代码:
return preg_replace_callback(
$pattern,
function($matches) use ($replace) {
return ((isset($replace[$matches[0]])) ? $replace[$matches[0]] : '');
},
nl2br(stripslashes($string))
);
我已经查阅了相关资料,但大多数示例都涉及基本的替换。这里有两个数组。
请注意,$replace区域中调用了一些方法。
我该如何解决这个问题?这是正确的方法吗?