理解 print_r 输出

发布于 2024-12-09 01:45:58 字数 109 浏览 0 评论 0原文

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

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

There are cases when the output for print_r is very complicated, long and hard to read, e.g. objects, nesting arrays, nesting objects, nesting arrays,...

Is there library or tools to help developers read this information? Something like the DOM inspector for HTML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆追雨的时光 2024-12-16 01:45:58

我在筛选 print_r 输出时经常使用此函数。这是一个非常棒的快速替代方案。

http://www.php.net/manual/en/function .print-r.php#90759 感谢 bob

<?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 文件中。

I regularly use this function when sifting through print_r output. It's a fantastic quick alternative.

http://www.php.net/manual/en/function.print-r.php#90759 Credit to bob

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

You can put it in your theme's template.php file if you're using Drupal as the tags imply.

蓝眼泪 2024-12-16 01:45:58

安装 Devel 模块 并使用 dpm() 函数代替 print_r...它将变量(无论是字符串、对象、数组等)的漂亮表示形式打印到标准 Drupal 消息区域中。

它利用了精彩的 Krumo 库,并且是完全交互式的(您可以展开/折叠对象/数组)。有些人可能会说为此使用

 标签(通常他们可能是对的),但由于 Drupal 已经存在此解决方案,如果不使用它,你会发疯的,

你将永远不会回去我保证再次使用 print_r

Install the Devel Module and use the dpm() function in place of print_r...it prints out a lovely representation of your variable (whether it be string, object, array, etc.) into the standard Drupal messages area.

It makes use of the wonderful Krumo Library and is fully interactive (you can expand/collapse objects/arrays). Some might say to use <pre> tags for this (and normally they might be right) but as this solution already exists for Drupal you'd be crazy not to use it

You'll never go back to using print_r again I promise!

落叶缤纷 2024-12-16 01:45:58

print_r 的手册页有很多可以帮助您的函数注释。

例如:

  • Federico Bricker 29-Jun-2009 11:02(帖子#91861)见下文。
  • Bob 08-May-2009 10:19 (Post #90759)
  • afisher8 at cox dot net 17-Dec-2008 04:53 (Post #87705)
  • 还有更多...

这是我用过几次的:

<?php 
function print_nice($elem,$max_level=10,$print_nice_stack=array()){ 
if(is_array($elem) || is_object($elem)){ 
    if(in_array(&$elem,$print_nice_stack,true)){ 
        echo "<font color=red>RECURSION</font>"; 
        return; 
    } 
    $print_nice_stack[]=&$elem; 
    if($max_level<1){ 
        echo "<font color=red>nivel maximo alcanzado</font>"; 
        return; 
    } 
    $max_level--; 
    echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>"; 
    if(is_array($elem)){ 
        echo '<tr><td colspan=2 style="background-color:#333333;"><strong><font color=white>ARRAY</font></strong></td></tr>'; 
    }else{ 
        echo '<tr><td colspan=2 style="background-color:#333333;"><strong>'; 
        echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>'; 
    } 
    $color=0; 
    foreach($elem as $k => $v){ 
        if($max_level%2){ 
            $rgb=($color++%2)?"#888888":"#BBBBBB"; 
        }else{ 
            $rgb=($color++%2)?"#8888BB":"#BBBBFF"; 
        } 
        echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">'; 
        echo '<strong>'.$k."</strong></td><td>"; 
        print_nice($v,$max_level,$print_nice_stack); 
        echo "</td></tr>"; 
    } 
    echo "</table>"; 
    return; 
} 
if($elem === null){ 
    echo "<font color=green>NULL</font>"; 
}elseif($elem === 0){ 
    echo "0"; 
}elseif($elem === true){ 
    echo "<font color=green>TRUE</font>"; 
}elseif($elem === false){ 
    echo "<font color=green>FALSE</font>"; 
}elseif($elem === ""){ 
    echo "<font color=green>EMPTY STRING</font>"; 
}else{ 
    echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem); 
} 
} 
?>

The manual page for print_r has a lot of comments with functions that could help you.

For example:

  • Federico Bricker 29-Jun-2009 11:02 (Post #91861) see below.
  • Bob 08-May-2009 10:19 (Post #90759)
  • afisher8 at cox dot net 17-Dec-2008 04:53 (Post #87705)
  • and many more ...

This is the one I used a few times:

<?php 
function print_nice($elem,$max_level=10,$print_nice_stack=array()){ 
if(is_array($elem) || is_object($elem)){ 
    if(in_array(&$elem,$print_nice_stack,true)){ 
        echo "<font color=red>RECURSION</font>"; 
        return; 
    } 
    $print_nice_stack[]=&$elem; 
    if($max_level<1){ 
        echo "<font color=red>nivel maximo alcanzado</font>"; 
        return; 
    } 
    $max_level--; 
    echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>"; 
    if(is_array($elem)){ 
        echo '<tr><td colspan=2 style="background-color:#333333;"><strong><font color=white>ARRAY</font></strong></td></tr>'; 
    }else{ 
        echo '<tr><td colspan=2 style="background-color:#333333;"><strong>'; 
        echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>'; 
    } 
    $color=0; 
    foreach($elem as $k => $v){ 
        if($max_level%2){ 
            $rgb=($color++%2)?"#888888":"#BBBBBB"; 
        }else{ 
            $rgb=($color++%2)?"#8888BB":"#BBBBFF"; 
        } 
        echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">'; 
        echo '<strong>'.$k."</strong></td><td>"; 
        print_nice($v,$max_level,$print_nice_stack); 
        echo "</td></tr>"; 
    } 
    echo "</table>"; 
    return; 
} 
if($elem === null){ 
    echo "<font color=green>NULL</font>"; 
}elseif($elem === 0){ 
    echo "0"; 
}elseif($elem === true){ 
    echo "<font color=green>TRUE</font>"; 
}elseif($elem === false){ 
    echo "<font color=green>FALSE</font>"; 
}elseif($elem === ""){ 
    echo "<font color=green>EMPTY STRING</font>"; 
}else{ 
    echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem); 
} 
} 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文