preg_replace函数中的/e修饰符已经过时,使用数组作为模式和替换。

3

我曾经有一段代码来模拟一些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区域中调用了一些方法。
我该如何解决这个问题?这是正确的方法吗?

前往https://dev59.com/RXvaa4cB1Zd3GeqPGrQa链接。 - Domain
我有点难以理解你试图做什么。你是在尝试在“use”部分传递两个数组吗? - Quixrick
不,这只是一个数组。我只是期望与以前相同的结果。 - Lucas
@Lucas 请试用一下这个库,它可以解决你所有与BBCode相关的问题:https://github.com/thunderer/Shortcode 。我是作者,如果你有任何问题,请告诉我,我会帮忙解决。 - Tomasz Kowalczyk
1个回答

0
经过大量的工作、测试和阅读,我找到了一个解决方案,希望能帮助其他人。
以下是我替换bbcode的解决方案
$bbcodes    = [
    '/\\n/' => '$this->setLineJump()',
    '/\\r/' => '$this->setReturn()',
    '/\[list\](.*?)\[\/list\]/is' => '$this->setList(\'\\1\')',
    '/\[b\](.*?)\[\/b\]/is' => '$this->setBold(\'\\1\')',
    '/\[strong\](.*?)\[\/strong\]/is' => '$this->setBold(\'\\1\')',
    '/\[i\](.*?)\[\/i\]/is' => '$this->setItalic(\'\\1\')',
    '/\[u\](.*?)\[\/u\]/is' => '$this->setUnderline(\'\\1\')',
    '/\[s\](.*?)\[\/s\]/is' => '$this->setStrike(\'\\1\')',
    '/\[del\](.*?)\[\/del\]/is' => '$this->setStrike(\'\\1\')',
    '/\[url=(.*?)\](.*?)\[\/url\]/is' => '$this->setUrl(\'\\1\',\'\\2\')',
    '/\[email=(.*?)\](.*?)\[\/email\]/is' => '$this->setEmail(\'\\1\',\'\\2\')',
    '/\[img](.*?)\[\/img\]/is' => '$this->setImage(\'\\1\')',
    '/\[color=(.*?)\](.*?)\[\/color\]/is' => '$this->setFontColor(\'\\1\',\'\\2\')',
    '/\[font=(.*?)\](.*?)\[\/font\]/is' => '$this->setFontFamiliy(\'\\1\',\'\\2\')',
    '/\[bg=(.*?)\](.*?)\[\/bg\]/is' => '$this->setBackgroundColor(\'\\1\',\'\\2\')',
    '/\[size=(.*?)\](.*?)\[\/size\]/is' => '$this->setFontSize(\'\\1\',\'\\2\')'
];

$string = stripslashes($string);

foreach ($bbcodes as $bbcode => $html) {
    $string = preg_replace_callback(
        $bbcode,
        function($matches) use($html) {
            return $this->getBbCode($matches, $html);
        },
        $string
    );
}

private function getBbCode($matches, $replace)
{
    if (isset($matches[1])) {

        $replacements   = [
            '\1' => isset($matches[1]) ? $matches[1] : '',
            '\2' => isset($matches[2]) ? $matches[2] : ''
        ];

        return eval('return ' . strtr($replace, $replacements) . ';');   
    } else {

        return eval('return ' . $replace . ';');   
    }
}

正如您所看到的,我正在使用对象,但您可以用自己的函数替换它们。真正重要的是,您可以逐个替换元素,只需对数组进行一些更改即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接