不建议使用Preg Replace,尝试修复 [英] Preg replace deprecated, attempting to fix
问题描述
我刚刚升级到PHP 7,并不断完善与不推荐使用的功能相关的错误,
I've just upgraded to PHP 7 and have been hammering out errors associated with deprecated functions, with much success.
不幸的是,我一直无法为在交互式可折叠javascript事物中查看php数组"代码修复新的preg替换方法.
Sadly, I've been having trouble fixing the new preg replace methodology for my "view php array in a interactive collapsable javascript thing" code.
以下代码:
function print_r_tree($data)
{
// capture the output of $this->print_r_tree
$out = print_r($data, true);
// replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
$out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">\\2</a><div id=\"'.\$id.'\" style=\"display: none;\">'", $out);
// replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
$out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);
// print the javascript function toggleDisplay() and then the transformed output
echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
}
生成此警告. 警告:preg_replace():不再支持/e修饰符,请使用preg_replace_callback代替
Generates this warning. Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
删除第一个"preg_replace"中的"e"会破坏javascript内容.我也尝试了一些preg_replace_callback事情.
Removing the "e" in the first "preg_replace", breaks the javascript thing. I've tried a few preg_replace_callback things as well.
我一直在尝试使用此链接替换preg_replace ()具有preg_replace_callback 的e修饰符,可帮助我了解发生了什么问题,但我认为我的问题因javascript而复杂化了.
I've been trying to use this link Replace preg_replace() e modifier with preg_replace_callback to help me understand what's broken, but I think my issue is complicated by the javascript.
就我的代码而言,我希望有人可以指导我完成这项工作吗?
I'm hoping someone might be able to walk me through this, with respect to my code?
谢谢.
推荐答案
e
修饰符位于您的第一个$out
变量中.要进行转换,您需要正确使用preg_replace_callback()
:
The e
modifier is in your first $out
variable. To transform that, you need to use correctly preg_replace_callback()
:
$out = preg_replace_callback('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iU', "callbackFunction", $out);
function callbackFunction($matches) {
return "'".$matches[1]."<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'".$matches[0]."'), 0, 7)).'\');\">".$matches[2]."</a><div id=\"'.\$id.'\" style=\"display: none;\">'";
}
请参见,在preg_replace_callback中,我们使用callbackFunction
定义了第二个参数,该字符串被解析为调用该函数,并且将带有匹配项的数组传递给它.因此,替换项是在callbackFunction()
函数中,而匹配项是matches[X]
.
See that in preg_replace_callback we define second parameter with callbackFunction
, that string is parsed to call that function and it passes an array with the matches. So the replacements were in callbackFunction()
function and the matches are matches[X]
.
更多信息:
http://php.net/manual/es/function .preg-replace-callback.php
祝你好运!
这篇关于不建议使用Preg Replace,尝试修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!