用preg_replace_callback替换已弃用的preg_replace/e [英] Replace deprecated preg_replace /e with preg_replace_callback
本文介绍了用preg_replace_callback替换已弃用的preg_replace/e的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$result = preg_replace(
"/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/iseU",
"CallFunction('\\1','\\2','\\3','\\4','\\5')",
$result
);
上面的代码在升级到PHP 5.5后给出了弃用警告:
The above code gives a deprecation warning after upgrading to PHP 5.5:
已弃用:preg_replace():/e修饰符已弃用,请改用preg_replace_callback
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead
如何用preg_replace_callback()
替换代码?
推荐答案
您可以使用匿名函数将匹配项传递给您的函数:
You can use an anonymous function to pass the matches to your function:
$result = preg_replace_callback(
"/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/isU",
function($m) { return CallFunction($m[1], $m[2], $m[3], $m[4], $m[5]); },
$result
);
除了速度更快之外,这还将正确处理字符串中的双引号.您当前使用/e
的代码会将双引号"
转换为\"
.
Apart from being faster, this will also properly handle double quotes in your string. Your current code using /e
would convert a double quote "
into \"
.
这篇关于用preg_replace_callback替换已弃用的preg_replace/e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文