开发者

Making sense of print_r output

开发者 https://www.devze.com 2023-04-11 18:42 出处:网络
There are cases when the output for print_r is very complicated, long and hard to read, e.g. objects, nesting arrays开发者_JS百科, nesting objects, nesting arrays,...

There are cases when the output for print_r is very complicated, long and hard to read, e.g. objects, nesting arrays开发者_JS百科, nesting objects, nesting arrays,...

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


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.


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!


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

  • http://www.php.net/manual/en/function.print-r.php

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); 
} 
} 
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号