1

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

可悲的是,我在为我的“在交互式可折叠 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 的东西。

我一直在尝试使用此链接Replace preg_replace() e modifier with preg_replace_callback来帮助我了解损坏的内容,但我认为我的问题因 javascript 而变得复杂。

关于我的代码,我希望有人能够引导我完成这个过程?

提前致谢。

4

2 回答 2

1

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 我们定义了第二个参数 with callbackFunction,该字符串被解析以调用该函数,并传递一个带有匹配项的数组。所以替换是callbackFunction()有效的,匹配是matches[X].

更多信息:

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

祝你好运!

于 2016-03-14T17:04:40.677 回答
0

这是原版Christian's fix的结合。

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;\">";
}
于 2019-10-30T19:11:24.630 回答