php – 理解print_r输出

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 理解print_r输出脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
有些情况下,PRint_r的输出非常复杂,冗长且难以阅读,例如对象,嵌套数组,嵌套对象,…

是否有图书馆或工具来帮助开发人员阅读这些信息?像DOM的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'),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输出全部内容,希望文章能够帮你解决php – 理解print_r输出所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。