Preg replace已弃用,正在尝试修复


Preg replace deprecated, attempting to fix

我刚刚升级到PHP7,一直在努力消除与不推荐使用的函数相关的错误,并取得了很大成功。

遗憾的是,我一直在为我的"在交互式可折叠javascript中查看php数组"代码修复新的preg替换方法时遇到了问题。

以下代码:

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代替

删除第一个"preg_replace"中的"e"会破坏javascript。我也试过一些preg_replace_callback的东西。

我一直在尝试使用这个链接将preg_Replace()e修饰符替换为preg_Replace_callback,以帮助我了解损坏了什么,但我认为我的问题因javascript而变得复杂。

我希望有人能带我完成这件事,关于我的代码?

提前谢谢。

e修饰符位于第一个$out变量中。要转换它,您需要正确使用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]

更多信息:

http://php.net/manual/es/function.preg-replace-callback.php

祝你好运!

这是原始版本与Christian修复版本的结合。

function print_r_tree($data)
{
    // capture the output of print_r
    $out = print_r($data, true);
    // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
    $out = preg_replace_callback('/([ 't]*)('[[^']]+'][ 't]*'='>[ 't]*[a-z0-9 't_]+)'n[ 't]*'(/iU', 'print_r_tree_callback', $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
    return '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."'n$out";
}
function print_r_tree_callback($matches) {
    $id = substr(md5(rand().$matches[0]), 0, 7);
    return "$matches[1]<a href='"javascript:toggleDisplay('$id');'">$matches[2]</a><div id='$id' style='"display: none;'">";
}