note 39364 modified in function.var-export by aidan
| From: | aidan@php.net | Date: | Wed, 09 Jun 2004 10:52:54 +0000 |
| Subject: | note 39364 modified in function.var-export by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-70632@lists.php.net to get a copy of this message | ||
[editors note - this function was based on the previous posts by the below users - those versions of
the user implementation were removed to reduce clutter]
anonymous at coward dot org
17-Oct-2003 07:32
sheimer at web dot de
26-Nov-2003 09:24
Alonso (alonso at mk dot com dot br)
24-Dec-2003 04:38
[original post]
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;
}
?>
--was--
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;
}
?>
http://www.php.net/manual/en/function.var-export.php