note 38757 added to function.var-dump

From: Date: Tue, 06 Jan 2004 06:34:18 +0000
Subject: note 38757 added to function.var-dump
Groups: php.notes 
Request: Send a blank email to php-notes+get-62982@lists.php.net to get a copy of this message
A word of caution using the fancy_vardump specified in the previous post. The contents of strings are printed directly to output. There is no conversion of HTML tags so formatting is easily broken, especially if a string variable contains "</table>" The following will fix the tag issue: <?php function fancy_vardump(&$vInput, $depth = 0) { $bgs = array ('#DDDDDD', '#C4F0FF', '#BDE9FF', '#FFF1CA'); $bg = &$bgs[$depth % sizeof($bgs)]; $s = "<table border='0' cellpadding='4' cellspacing='1'><tr><td style='background: none $bg; text-align: left; "; if (is_int($vInput)) { $s .= "'>"; $s .= sprintf('int (<b>%d</b>)', intval($vInput)); } else if (is_float($vInput)) { $s .= "'>"; $s .= sprintf('float (<b>%f</b>)', doubleval($vInput)); } else if (is_string($vInput)) { $tags = Array('<','>'); // Lines Changed to $entities = Array('&lt;','&gt;'); // Clean up HTML tags $cleaned_vInput = str_replace($tags,$entities,$vInput); // in strings $s .= "'>"; $s .= sprintf('string[%d] (<b>"%s"</b>)', strlen($vInput), $cleaned_vInput); } else if (is_bool($vInput)) { $s .= "'>"; $s .= sprintf('bool (<b>%s</b>)', ($vInput === true ? 'true' : 'false')); } else if (is_resource($vInput)) { $s .= "'>"; $s .= sprintf('resource (<b>%s</b>)', get_resource_type($vInput)); } else if (is_null($vInput)) { $s .= "'>"; $s .= sprintf('null'); } else if (is_array($vInput)) { $s .= "border-bottom: solid 2px black;'>"; $s .= sprintf('array[%d]', count($vInput)); $s .= "</td></tr><tr><td style='background: none $bg; text-align: left;'>" . "<table border='0' cellpadding='4' cellspacing='1'>"; foreach ($vInput as $vKey => $vVal) { $s .= '<tr>'; $s .= "<td style='background-color: $bg; text-align: left;'>" . sprintf('<b>%s%s%s</b>', ((is_int($vKey)) ? '' : '"'), $vKey, ((is_int($vKey)) ? '' : '"')) . '</td>'; $s .= "<td style='background-color: $bg; text-align: left;'>=></td>"; $s .= "<td style='background-color: $bg; text-align: left;'>" . fancy_vardump($vVal, $depth+1) . '</td>'; $s .= '</tr>'; } $s .= '</table>'; } else if (is_object($vInput)) { $s .= "border-bottom: solid 2px black;'>"; $s .= sprintf('object (<b>%s</b>)', get_class($vInput)); $s .= "</td></tr><tr><td style='background: none $bg; text-align: left;'>" . "<table border='0' cellpadding='4' cellspacing='1'>"; foreach (get_object_vars($vInput) as $vKey => $vVal) { $s .= '<tr>'; $s .= "<td style='background-color: $bg; text-align: left;'>" . sprintf('<b>%s%s%s</b>', ((is_int($vKey)) ? '' : '"'), $vKey, ((is_int($vKey)) ? '' : '"')) . '</td>'; $s .= "<td style='background-color: $bg; text-align: left;'>=></td>"; $s .= "<td style='background-color: $bg; text-align: left;'>" . fancy_vardump($vVal, $depth+1) . '</td>'; $s .= '</tr>'; } $s .= '</table>'; } else { $s .= "'>"; $s .= sprintf('<b>unhandled (gettype() reports "%s")', gettype($vInput)); } $s .= '</td></tr></table>'; return $s; } ?> This still does not address the issue of recursive arrays such as $GLOBALS. ---- Manual Page -- http://www.php.net/manual/en/function.var-dump.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38757 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38757&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38757&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#62982) next »