note 101593 deleted from function.print-r by danbrown

From: Date: Tue, 28 Dec 2010 18:31:38 +0000
Subject: note 101593 deleted from function.print-r by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-175285@lists.php.net to get a copy of this message
Note Submitter: lsiq at papotam com ---- // some little fixes to Bob's great function // (perfomance, and null key) // thanks Bob, it is realy lovely. <?php function print_r_tree($data) { // capture the output of print_r $out = print_r($data, true); $cid = 1; // fixes "[] =>" (null key) $out = preg_replace("/\[]\s+=>/", "[this text is not a key, do you have a key like this] =>", $out); // 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(\'x'.(\$id = \$cid++).'\');\">\\2</a><div id=\"x'.\$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); $out = str_replace("[this text is not a key, do you have a key like this] =>", "[] =>", $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"; } ?>

« previous php.notes (#175285) next »