【发布时间】:2016-05-08 06:26:25
【问题描述】:
我曾经有这个代码块来模拟某种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 而不是在
我尝试了几种方法,但到目前为止没有任何效果。
这是我目前尝试的代码:
return preg_replace_callback(
$pattern,
function($matches) use ($replace) {
return ((isset($replace[$matches[0]])) ? $replace[$matches[0]] : '');
},
nl2br(stripslashes($string))
);
我一直在阅读,但大多数示例都与基本替换有关,这里我有两个数组。
请注意有些方法从 $replace 区域被调用。
我该如何解决这个问题?这是正确的方法吗?
【问题讨论】:
-
我很难理解您要做什么。是您试图在
use部分传递两个数组吗? -
不,它只是一个数组。我只是期待和以前一样的结果。
-
@Lucas 请试试这个库,它将解决您所有与 BBCode 相关的问题:github.com/thunderer/Shortcode。我是作者,如果您有任何问题,请告诉我,我会提供帮助。
标签: php arrays preg-replace deprecated preg-replace-callback