note 39364 added to function.var-export
| From: | RickardAndersson at rn2 dot php dot net | Date: | Mon, 26 Jan 2004 02:53:52 +0000 |
| Subject: | note 39364 added to function.var-export | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64185@lists.php.net to get a copy of this message | ||
And here's yet another version! This one escapes singlequote and backslash correctly and uses
two spaces instead of tabs to indent the output. It's output is, as far as I can see, identical
to that of the native function. It's based on the one by Alonso Andres below, but I removed a
few variables to make it as lean as possible.
<?php
function var_export()
{
$args = func_get_args();
$indent = (isset($args[2])) ? $args[2] : '';
if (is_array($args[0]))
{
$output = 'array ('."\n";
foreach ($args[0] as $k => $v)
{
if (is_numeric($k))
$output .= $indent.' '.$k.' => ';
else
$output .= $indent.' \''.str_replace('\'', '\\\'',
str_replace('\\', '\\\\', $k)).'\' => ';
if (is_array($v))
$output .= var_export($v, true, $indent.' ');
else
{
if (gettype($v) != 'string' && !empty($v))
$output .= $v.','."\n";
else
$output .= '\''.str_replace('\'', '\\\'',
str_replace('\\', '\\\\', $v)).'\','."\n";
}
}
$output .= ($indent != '') ? $indent.'),'."\n" : ')';
}
else
$output = $args[0];
if ($args[1] == true)
return $output;
else
echo $output;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.var-export.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39364
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39364&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39364&report=yes
Search -- http://master.php.net/manage/user-notes.php