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'), 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,drupal,drupal-modules 来源: https://codeday.me/bug/20190613/1235608.html