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

preg_replace改写preg_replace_callback的有关问题

WBOY
WBOYOriginal
2016-06-13 12:06:09882browse

preg_replace改写preg_replace_callback的问题
原代码如下:

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

原本服务器是php5.2,现在升级成php5.5之后,发现程序出错
------解决方案--------------------
        $pattern = array(<br />            '/<!--[^><br><font color='#FF8000'>------解决方案--------------------</font><br>\n]*?({.+?})[^<<br><font color='#FF8000'>------解决方案--------------------</font><br>{<br><font color='#FF8000'>------解决方案--------------------</font><br>\n]*?-->/', // 替换smarty注释<br />            '/<!--[^<<br><font color='#FF8000'>------解决方案--------------------</font><br>><br><font color='#FF8000'>------解决方案--------------------</font><br>{<br><font color='#FF8000'>------解决方案--------------------</font><br>\n]*?-->/',               // 替换不换行的html注释<br />            '/(href=["<br><font color='#FF8000'>------解决方案--------------------</font><br>\'])\.\.\/(.*?)(["<br><font color='#FF8000'>------解决方案--------------------</font><br>\'])/i',  // 替换相对链接<br />            '/((?:background<br><font color='#FF8000'>------解决方案--------------------</font><br>src)\s*=\s*["<br><font color='#FF8000'>------解决方案--------------------</font><br>\'])(?:\.\/<br><font color='#FF8000'>------解决方案--------------------</font><br>\.\.\/)?(images\/.*?["<br><font color='#FF8000'>------解决方案--------------------</font><br>\'])/is', // 在images前加上 $tmp_dir<br />            '/((?:background<br><font color='#FF8000'>------解决方案--------------------</font><br>background-image):\s*?url\()(?:\.\/<br><font color='#FF8000'>------解决方案--------------------</font><br>\.\.\/)?(images\/)/is', // 在images前加上 $tmp_dir<br />            );<br />        $replace = array(<br />            '\1',<br />            '',<br />            '\1\2\3',<br />            '\1' . $tmp_dir . '\2',<br />            '\1' . $tmp_dir . '\2',<br />            );<br /><br />        $source = preg_replace($pattern, $replace, $source);<br />        return preg_replace_callback('/{nocache}(.+?){\/nocache}/is', function($m) {<br />            return '{insert name="nocache" ' . $this->echash . base64_encode($m[1]) . '}';<br />        }, $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