当前位置:Gxlcms > PHP教程 > preg_replace改写preg_replace_callback的有关问题

preg_replace改写preg_replace_callback的有关问题

时间:2021-07-01 10:21:17 帮助过:5人阅读

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

$pattern = array(
'//', // 替换smarty注释
'//', // 替换不换行的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(
'//', // 替换smarty注释
'//', // 替换不换行的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);

人气教程排行