在某些情况下,print_r 的输出非常复杂、冗长且难以阅读,例如对象,嵌套数组,嵌套对象,嵌套数组,...

是否有库或工具可以帮助开发人员阅读这些信息?类似于 HTML 的 DOM 检查器?

最佳答案

我在筛选 print_r 输出时经常使用这个函数。这是一个很棒的快速选择。

http://www.php.net/manual/en/function.print-r.php#90759 归功于鲍勃

<?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";
}
?>

如果你使用 Drupal 作为标签,你可以把它放在你主题的 template.php 文件中。

关于php - 理解 print_r 输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7712912/

10-13 02:44