note 38757 deleted from function.var-dump by sniper
| From: | sniper@php.net | Date: | Sun, 11 Jan 2004 15:39:19 +0000 |
| Subject: | note 38757 deleted from function.var-dump by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63318@lists.php.net to get a copy of this message | ||
Note Submitter: chris at americanfirstmtg dot com
----
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('<','>'); // 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.