note 97901 added to function.print-r
| From: | Levofski at osu1 dot php dot net | Date: | Fri, 14 May 2010 12:19:53 +0000 |
| Subject: | note 97901 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169240@lists.php.net to get a copy of this message | ||
Hi all, here is a function that will display any variable, including objects, as XML so you can
expand and collapse in browsers etc. Note that it will output all variables, not just public ones.
<?php
/**
* Will output in a similar form to print_r, but the nodes are xml so can be collapsed 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) with
'</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;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 213.131.162.123
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/97901
Del: integrated -- https://master.php.net/note/delete/97901/integrated
Del: useless -- https://master.php.net/note/delete/97901/useless
Del: bad code -- https://master.php.net/note/delete/97901/bad+code
Del: spam -- https://master.php.net/note/delete/97901/spam
Del: non-english -- https://master.php.net/note/delete/97901/non-english
Del: in docs -- https://master.php.net/note/delete/97901/in+docs
Del: other reasons-- https://master.php.net/note/delete/97901
Reject -- https://master.php.net/note/reject/97901
Search -- https://master.php.net/manage/user-notes.php