php - preg_replace/e 修饰符被弃用,使用数组作为模式和替换

标签 php arrays preg-replace deprecated preg-replace-callback

我曾经用这段代码来模拟某种 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 并且我在这里遇到错误,它曾经完美地工作,这是我遇到的错误:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in

我尝试了几种方法,但到目前为止没有任何效果。

这是我到目前为止尝试过的代码:

return preg_replace_callback(
    $pattern,
    function($matches) use ($replace) {
        return ((isset($replace[$matches[0]])) ? $replace[$matches[0]] : '');
    },
    nl2br(stripslashes($string))
);

我一直在阅读,但大多数示例都与基本替换相关,这里我有两个数组。

注意有一些方法是从$replace 区域调用的。

我该如何解决这个问题?这是正确的方法吗?

最佳答案

经过大量的工作、测试和阅读,我得出了一个解决方案,希望它能帮助其他人。

这是我用来替换 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 . ';');   
    }
}

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

关于php - preg_replace/e 修饰符被弃用,使用数组作为模式和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102819/

相关文章:

php - 修改并添加到 MySQL 结果

PHP 数据从 MySQL 到每个条目的单独页面

php - LiveSearch PHP、JavaScript 多领域使用

PHP - 获取带有数字索引的数组值

PHP FPM 终止 Cloud Run 中的每个请求

C# 将数组的元素移动到特定点,并将特定元素拉到前面

php - 通过访问器方法在 $_SESSION 中设置数组值

php - 修改抓取网页中包含的 JavaScript 的最佳方法是什么?

php - 正则表达式:如何查找后跟非字母数字的字符串

php - 简单的PHP替换