<?php
/*
* Dumper v1.1 - Reverse engineering data structures.
* + patch for dumping object properties (Steven Haryanto
* <steven@haryan.to>
* + patch that replaces enquote() with builtin functions
*
* Author: Jan Fredrik Leversund <kluzz@radical.org>
*
*
* This code will create a string representation of a data
* structure, suitable for debugging or for storage and retrieval.
*
* Note however that classes aren't handled very well at all. One
* of the reasons is that by PHP3.0.7 there seems to be no way of
* telling which class an object belongs to, only that it is an
* object. Also, I'm not quite sure how to handle objects even if
* an identifying function existed. Basically, if your intentions
* are to use this for storage, avoid stuffing objects into your
* structures. The data are retained, but methods are lost.
*
* A word about strings: All but the most common characters are
* converted to hexadecimal escaped characters, to avoid most of
* the quirks in string representation.
*
* The usage is quite simple:
*
* string Dumper(mixed structure, string [name], string [indstr])
*
* 'structure' - The actual data structure you want dumped.
* 'name' - The name given the reverse engineered
* structure. The default is 'var'. Optional.
* 'indstr' - The string used to for indentation. The
* default is four spaces. Other typical values
* may be a single tab or some other amount of
* spaces. Optional.
*
* An example:
*
* require "dumper.phl";
* $blah = array(9, "hello", 5, 3, 1);
* echo Dumper($blah, "newblah", " ");
*
* This gives the following output:
*
* $newblah = array (
* 0 => 9,
* 1 => "hello",
* 2 => 5,
* 3 => 3,
* 4 => 1
* );
*
*
* Known bugs:
*
* - Objects are not handled well, only as well as the serialize()
* and unserialize() functions can do. Don't use Dumper() if you
* are seriously thinking about storing objects for later use.
*
*/
/*
* string spc(string str, int lvl)
* - Used to generate a string of spaces.
*/
function spc($str, $lvl) {
unset($sp);
for ($i = 0;$i < $lvl;$i++) {
$sp .= $str;
}
return $sp;
}
/*
* string Dumper(mixed structure, string [name], string [indstr], int [indlvl])
* - Reverse engineers a structure. 'structure' is whatever
* variable you want to stringify.
*/
function Dumper($s, $name = "var", $indstr = " ", $indlvl = 0) {
unset($str);
$newname = $indlvl ? $name : "\$$name";
if (is_array($s)) {
$str .= spc($indstr,$indlvl) . "$newname =" . ($indlvl ? ">" : "") . " array (\n";
reset($s);
$count = count($s);
for ($i = 0;$i < $count;$i++) {
$str .= Dumper(current($s), key($s), $indstr, $indlvl + 1);
next($s);
if ($i < ($count - 1)) {
$str .= ",";
}
$str .= "\n";
}
$str .= spc($indstr, $indlvl) . ")";
if (!$indlvl) {
$str .= ";\n";
}
} elseif (is_object($s)) {
$ser = serialize($s);
$len = strlen($ser);
$arr = (array)$s;
$ser_a = serialize($arr);
preg_match('/^(O:[^:]+:[^:]+:[^:]+:)/', $ser, $header);
$header = $header[0];
preg_match('/^(a:[^:]+:)/', $ser_a, $header_a);
$header_a = $header_a[0];
$str .= spc($indstr,$indlvl) . "$newname =" . ($indlvl ? ">" : "") .
" unserialize(preg_replace('/^$header_a/', '$header', serialize(array( \n";
reset($arr);
$count = count($arr);
for ($i = 0;$i < $count;$i++) {
$str .= Dumper(current($arr), key($arr), $indstr, $indlvl + 1);
next($arr);
if ($i < ($count - 1)) {
$str .= ",";
}
$str .= "\n";
}
$str .= spc($indstr, $indlvl) . "))))";
if (!$indlvl) {
$str .= ";\n";
}
} elseif (is_string($s)) {
$str .= spc($indstr, $indlvl) . "$newname => \"" . addcslashes(addslashes($s), "\0..\37!@\177..\377") . "\"";
} elseif (!strcmp(gettype($s), "user function")) {
$str .= spc($indstr, $indlvl) . "$newname()";
} else {
$str .= spc($indstr, $indlvl) . "$newname => $s";
}
return $str;
}
?>
|