如何将print_r主题转换为可折叠行?

tim*_*com 8 php

print_r,返回页面和代码页; 滚动页面以使孩子与父母匹配太难了,甚至用<pre>标签包裹.

有没有办法将print_r主题化为可折叠字段.也许是一个在线生成器,我可以在其中发布内容print_r($array);并获得可折叠的字段表.

例如,在Drupal中,有一个名为Devel的模块就是这样做的. Devel截图

dei*_*zel 10

除非我遗漏了什么,答案就在你的截图中:http://krumo.sourceforge.net/


edw*_*dmp 8

为什么不把它作为JSON输出然后粘贴在http://jsonviewer.stack.hu/上,这使得它可读并且可折叠!

json_encode($array);
Run Code Online (Sandbox Code Playgroud)

例: 在此输入图像描述


tim*_*com 8

感谢这篇文章,这是一个解决方案.

在之前插入以下功能print_r.

<?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";
}
?>
Run Code Online (Sandbox Code Playgroud)

然后,替换print_r(),用print_r_tree(); 像这样:

<pre><?php echo print_r_tree(get_defined_vars()); ?></pre>
Run Code Online (Sandbox Code Playgroud)

不要忘记<pre>标签.

结果看起来与print_r()函数的结果相同,只是现在数组是可折叠的.