| px | top | add code | search | signup | login | help |
<?php
/*
* Dumper v1.1 - Reverse engineering data structures.
*
* 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 enquote(string str)
* - Make a string sort of 7bit and PHP safe. This should
* be implemented in PHP3 as a build in function, as I
* suspect that this one is pretty slow. Besides, I'm
* probably not the only one needing this piece of code.
*/
function enquote($str) {
unset($out);
for ($i = 0;$i < strlen($str); $i++) {
$char = substr($str, $i, 1);
if (eregi("[^ a-z0-9.,+=()/&%#!'~^:;<>{}-]", $char, $regs)) {
if (!strcmp($char, "\\")) {
$out .= "\\\\";
} elseif (!strcmp($char, "\n")) {
$out .= "\\n";
} elseif (!strcmp($char, "\r")) {
$out .= "\\r";
} elseif (!strcmp($char, "\t")) {
$out .= "\\t";
} elseif (!strcmp($char, "\"")) {
$out .= "\\\"";
} elseif (!strcmp($char, "[") || !strcmp($char, "]")) {
$out .= $char;
} else {
$out .= sprintf("\\x%x", ord($char));
}
} else {
$out .= $char;
}
}
return $out;
}
/*
* 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);
$str .= spc($indstr,$indlvl) . "$newname =" . ($indlvl ? ">" : "") . " unserialize(\n";
for ($i = 0;$len > 0;$i += 42) {
$tmp = substr($ser, $i, 42);
$len -= 42;
$str .= spc($indstr, $indlvl + 1) . "\"" . enquote($tmp) . "\"";
$str .= $len <= 0 ? "\n" : " .\n";
}
$str .= spc($indstr, $indlvl) . ")";
if (!$indlvl) {
$str .= ";\n";
}
} elseif (is_string($s)) {
$str .= spc($indstr, $indlvl) . "$newname => \"" . enquote($s) . "\"";
} elseif (!strcmp(gettype($s), "user function")) {
$str .= spc($indstr, $indlvl) . "$newname()";
} else {
$str .= spc($indstr, $indlvl) . "$newname => $s";
}
return $str;
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.