How to adjust preg_replace after PHP7.0 abandons it
高洛峰
高洛峰 2017-05-16 13:09:06
0
3
580
$options = Typecho_Widget::widget('Widget_Options');
$sUrl = str_ireplace('/', '\/', rtrim($options->siteUrl, '/'));
$preg = '#(<a .*?href=")(?!' . $sUrl . ')([^"]+)"(.*?<\/a>)#ise';
$text = preg_replace($preg, "stripslashes('$1') . '$options->siteUrl' . 'go.html?url=' . base64_encode('$2') . '\" target=\"_blank\ "' . stripslashes('$3')", $text);

After the code was put into PHP7.0, an error was reported. According to the prompts, I learned that preg_replace was abandoned and had to be replaced by preg_replace_callback. How to do it?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
Peter_Zhu

It’s not that preg_replace is abandoned, it’s the /e modifier that is abandoned.

I think it should probably be changed like this

preg_replace_callback($preg,
    function ($matches) {
        return stripslashes($matches[1]).$options->siteUrl.'go.html?url='.base64_encode($matches[2]).'\" target=\"_blank\"'.stripslashes($matches[3]);
    });
迷茫

Where can I see that this function has been abandoned?
http://php.net/manual/zh/func...
Support (PHP 4, PHP 5, PHP 7)
Post an error message

黄舟

Preg-prefixed functions are not obsolete, but eregi-prefixed functions are obsolete

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template