Arrays 带有PHP7的可折叠打印树(不带preg_替换()和/e)

Arrays 带有PHP7的可折叠打印树(不带preg_替换()和/e),arrays,tree,preg-replace,preg-replace-callback,collapsable,Arrays,Tree,Preg Replace,Preg Replace Callback,Collapsable,为了打印一棵可折叠树,我目前正在使用他的代码,它使用preg\u replace()和/e修饰符,修饰符在PHP7中折旧: <?php function print_r_tree($data) { // capture the output of print_r $out = print_r($data, true); // replace something like '[element] => <newline> (' with <a

为了打印一棵可折叠树,我目前正在使用他的代码,它使用
preg\u replace()
/e
修饰符,修饰符在PHP7中折旧:

<?php
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('/([ \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";
}
?>

您需要输入变量$id值,请尝试以下操作:

function callbackFunction($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;\">'";
}
function callbackFunction($matches){
$id=substr(md5(rand().$matches[0]),0,7);
返回“$matches[1]”;
}
function callbackFunction($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;\">'";
}