Home  >  Article  >  Backend Development  >  preg_replace改写preg_replace_callback的问题

preg_replace改写preg_replace_callback的问题

WBOY
WBOYOriginal
2016-06-23 13:50:13994browse

原代码如下:

        $pattern = array(            '/<!--[^>|\n]*?({.+?})[^<|{|\n]*?-->/', // 替换smarty注释            '/<!--[^<|>|{|\n]*?-->/',               // 替换不换行的html注释            '/(href=["|\'])\.\.\/(.*?)(["|\'])/i',  // 替换相对链接            '/((?:background|src)\s*=\s*["|\'])(?:\.\/|\.\.\/)?(images\/.*?["|\'])/is', // 在images前加上 $tmp_dir            '/((?:background|background-image):\s*?url\()(?:\.\/|\.\.\/)?(images\/)/is', // 在images前加上 $tmp_dir            '/{nocache}(.+?){\/nocache}/ise', //无缓存模块            );        $replace = array(            '\1',            '',            '\1\2\3',            '\1' . $tmp_dir . '\2',            '\1' . $tmp_dir . '\2',            "'{insert name=\"nocache\" ' . '" . $this->_echash . "' . base64_encode('\\1') . '}'",            );        return preg_replace($pattern, $replace, $source);

原本服务器是php5.2,现在升级成php5.5之后,发现程序出错


回复讨论(解决方案)

        $pattern = array(            '/<!--[^>|\n]*?({.+?})[^<|{|\n]*?-->/', // 替换smarty注释            '/<!--[^<|>|{|\n]*?-->/',               // 替换不换行的html注释            '/(href=["|\'])\.\.\/(.*?)(["|\'])/i',  // 替换相对链接            '/((?:background|src)\s*=\s*["|\'])(?:\.\/|\.\.\/)?(images\/.*?["|\'])/is', // 在images前加上 $tmp_dir            '/((?:background|background-image):\s*?url\()(?:\.\/|\.\.\/)?(images\/)/is', // 在images前加上 $tmp_dir            );        $replace = array(            '\1',            '',            '\1\2\3',            '\1' . $tmp_dir . '\2',            '\1' . $tmp_dir . '\2',            );        $source = preg_replace($pattern, $replace, $source);        return preg_replace_callback('/{nocache}(.+?){\/nocache}/is', function($m) {            return '{insert name="nocache" ' . $this->echash . base64_encode($m[1]) . '}';        }, $source);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn