Important alert: (current site time 7/15/2013 10:19:11 AM EDT)
 

VB icon

Dumper

Email
Submitted on: 10/29/2000 1:39:26 PM
By: PHP Code Exchange  
Level: Beginner
User Rating: By 1 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 8317
 
     Dumper is a PHP3 implementation of Perl's Data::Dumper. It reverse engineers a data structure into a human readable and PHP3 parseable format, suitable for debugging or storing and retrieving. It still needs some work on class objects. by Thomas Hansmann.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Dumper
// Description:Dumper is a PHP3 implementation of Perl's Data::Dumper. It reverse engineers a data structure into a human readable and PHP3 parseable format, suitable for debugging or storing and retrieving. It still needs some work on class objects. by Thomas Hansmann.
// By: PHP Code Exchange
//**************************************

<?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;
}
?>


Other 192 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.