note 69506 added to function.print-r

From: Date: Fri, 08 Sep 2006 08:20:12 +0000
Subject: note 69506 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-116889@lists.php.net to get a copy of this message
Sometimes print_r produces large output, especially when the data hierarchy is too deep. It is very difficult to analyze the dump about 1Mb length. It would be great to have some way to fold arrays and objects and to look deeper into hierarchy only on demand. Here is the solution. Just pass the print_r output to debug_var function: debug_var('title', print_r($var, true)); and it will produce nice html with folding option. <?php ob_start(); ?> Locals: Array ( [arr] => Array ( [0] => Bon Object ( [n] => id [v] => 1 [dv] => [dn] => ) ) [b] => Bon Object ( [n] => id [v] => 1 [dv] => [dn] => ) [k] => 0 [row] => Array ( [aid] => 1 [bonus] => spell.id: 125; [req] => [brcache] => [auto] => 0 ) [sp] => ) <?php $str = ob_get_contents(); ob_end_clean(); debug_var('locals', $str); function debug_var($name,$data) { $captured = preg_split("/\r?\n/",$data); print "<script>function toggleDiv(num){ var span = document.getElementById('d'+num); var a = document.getElementById('a'+num); var cur = span.style.display; if(cur == 'none'){ a.innerHTML = '-'; span.style.display = 'inline'; }else{ a.innerHTML = '+'; span.style.display = 'none'; } }</script>"; print "<b>$name</b>\n"; print "<pre>\n"; foreach($captured as $line) { print debug_colorize_string($line)."\n"; } print "</pre>\n"; } function next_div($matches) { static $num = 0; ++$num; return "$matches[1]<a id=a$num href=\"javascript: toggleDiv($num)\">+</a><span id=d$num style=\"display:none\">("; } /** * colorize a string for pretty display * * @access private * @param $string string info to colorize * @return string HTML colorized * @global */ function debug_colorize_string($string) { $string = preg_replace("/\[(\w*)\]/i", '[<font color="red">$1</font>]', $string); $string = preg_replace_callback("/(\s+)\($/", 'next_div', $string); $string = preg_replace("/(\s+)\)$/", '$1)</span>', $string); /* turn array indexes to red */ /* turn the word Array blue */ $string = str_replace('Array','<font color="blue">Array</font>',$string); /* turn arrows graygreen */ $string = str_replace('=>','<font color="#556F55">=></font>',$string); return $string; } ?> This example uses ideas from this article: http://www.zend.com/zend/tut/tutorial-DebugLib.php ---- Server IP: 195.222.164.18 Probable Submitter: 217.10.47.86 ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/69506 Del: integrated -- https://master.php.net/note/delete/69506/integrated Del: useless -- https://master.php.net/note/delete/69506/useless Del: bad code -- https://master.php.net/note/delete/69506/bad+code Del: spam -- https://master.php.net/note/delete/69506/spam Del: non-english -- https://master.php.net/note/delete/69506/non-english Del: in docs -- https://master.php.net/note/delete/69506/in+docs Del: other reasons-- https://master.php.net/note/delete/69506 Reject -- https://master.php.net/note/reject/69506 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116889) next »