Sunteți pe pagina 1din 2

<?

php
/**
* Will output in a similar form to print_r, but the nodes are xml so can be col
lapsed in browsers
*
* @param mixed $mixed
*/
function print_r_xml($mixed)
{
// capture the output of print_r
$out = print_r($mixed, true);
// Replace the root item with a struct
// MATCH : '<start>element<newline> ('
$root_pattern = '/[ \t]*([a-z0-9 \t_]+)\n[ \t]*\(/i';
$root_replace_pattern = '<struct name="root" type="\\1">';
$out = preg_replace($root_pattern, $root_replace_pattern, $out, 1);
// Replace array and object items structs
// MATCH : '[element] => <newline> ('
$struct_pattern = '/[ \t]*\[([^\]]+)\][ \t]*\=\>[ \t]*([a-z0-9 \t_]+)\n[ \t]
*\(/miU';
$struct_replace_pattern = '<struct name="\\1" type="\\2">';
$out = preg_replace($struct_pattern, $struct_replace_pattern, $out);
// replace ')' on its own on a new line (surrounded by whitespace is ok) wit
h '</var>
$out = preg_replace('/^\s*\)\s*$/m', '</struct>', $out);
// Replace simple key=>values with vars
// MATCH : '[element] => value<newline>'
$var_pattern = '/[ \t]*\[([^\]]+)\][ \t]*\=\>[ \t]*([a-z0-9 \t_\S]+)/i';
$var_replace_pattern = '<var name="\\1">\\2</var>';
$out = preg_replace($var_pattern, $var_replace_pattern, $out);
$out = trim($out);
$out='<?xml version="1.0"?><data>'.$out.'</data>';
return $out;
}
?>
<?php
function print_r_html($arr, $style = "display: none; margin-left: 10px;")
{ static $i = 0; $i++;
echo "\n<div id=\"array_tree_$i\" class=\"array_tree\">\n";
foreach($arr as $key => $val)
{ switch (gettype($val))
{ case "array":
echo "<a onclick=\"document.getElementById('";
echo array_tree_element_$i."').style.display = ";
echo "document.getElementById('array_tree_element_$i";
echo "').style.display == 'block' ?";
echo "'none' : 'block';\"\n";
echo "name=\"array_tree_link_$i\" href=\"#array_tree_link_$i\">".htmlspe
cialchars($key)."</a><br />\n";
echo "<div class=\"array_tree_element_\" id=\"array_tree_element_$i\" st
yle=\"$style\">";
echo print_r_html($val);
echo "</div>";
break;
case "integer":
echo "<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."
</i><br />";
break;
case "double":
echo "<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."
</i><br />";
break;
case "boolean":
echo "<b>".htmlspecialchars($key)."</b> => ";
if ($val)
{ echo "true"; }
else
{ echo "false"; }
echo "<br />\n";
break;
case "string":
echo "<b>".htmlspecialchars($key)."</b> => <code>".htmlspecialchars($val
)."</code><br />";
break;
default:
echo "<b>".htmlspecialchars($key)."</b> => ".gettype($val)."<br />";
break; }
echo "\n"; }
echo "</div>\n"; }
?>

S-ar putea să vă placă și