Understanding print_r output

There are times when the output for print_r is very complex, long and hard to read, for example. objects, nested arrays, nesting objects, nesting arrays, ...

Are there any library or tools to help developers read this information? Something like a DOM inspector for HTML?

+4
source share
3 answers

I regularly use this function when sifting through print_r output. This is a fantastic quick alternative.

http://www.php.net/manual/en/function.print-r.php#90759 Credit 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 the theme theme.php file if you use Drupal, as tags imply.

+5
source

Install the Devel Module and use dpm() instead of print_r ... it prints a beautiful view of your variable (be it a string, object, array, etc.) into the standard Drupal message area.

It uses the wonderful Krumo Library and is fully interactive (you can expand / collapse objects / arrays). Some may say that the <pre> tags are used for this (and, as a rule, they may be correct), but since this solution already exists for Drupal, you would be crazy not to use it

You will never return to using print_r again, I promise!

+2
source

The print_r page has many comments with features that may help you.

For instance:

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

I have used this several 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
source

All Articles