note 42500 deleted from function.var-export by nlopess
| From: | nlopess@php.net | Date: | Wed, 19 May 2004 15:34:44 +0000 |
| Subject: | note 42500 deleted from function.var-export by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69833@lists.php.net to get a copy of this message | ||
Note Submitter: Eugene php (at) burtsev .dot. com
----
Here's a bit of code I use to output variables into the files
the use:
WriteFile($path[, $variable [, $variable , ... ]])
Note that path should be full (from the root) and variables can be passed in two ways:
If variable is global, the name in the form of a string should be enough, in any other case actual
variable should be passed after the name
exaple: WriteFile('/temp/test.php', 'globalvar', 'notglovar',
$notglovar, ...)
Code:
if (!function_exists("var_export")) {
function var_export($array, $return = false, $tabs = "")
{
switch(gettype($array))
{
case "array":
$out .= "array(\n";
$keys = array_keys($array);
$num = count($keys);
for ($i = 0; $i < $num; $i++)
{
if (is_string($keys[$i]))
$out .= $tabs . "\t" . "'$keys[$i]' => ";
else
$out .= $tabs . "\t" . $keys[$i] . ' => ';
if (!isset($array[$keys[$i]]["GLOBALS"]["GLOBALS"]["GLOBALS"]))
{
switch(gettype($array[$keys[$i]]))
{
case "array":
$out .= var_export($array[$keys[$i]], true, $tabs . "\t");
break;
case "string": case "NULL":
$out .= "'" . str_replace('\'', '\\\'',
str_replace('\\', '\\\\', $array[$keys[$i]])) . "'";
break;
default:
$out .= $array[$keys[$i]];
break;
}
} else
$out .= '&$GLOBALS';
$out .= ($i + 1 == $num ? "\n" : ",\n");
}
$out .= $tabs . ')';
break;
case "string": case "NULL":
$out = "'" . str_replace('\'', '\\\'',
str_replace('\\', '\\\\', $array)) . "'";
break;
default:
$out = $array;
break;
}
if ($return)
return $out;
echo $out;
}
}
function WriteFile($path)
{
$file = fopen($path,"w");
$output="<?php // File is autogenerated, DO NOT EDIT!\n";
$args = func_get_args();
for ($i = 1; $i < count($args); $i++)
if (isset($args[$i + 1]) && !is_string($args[$i + 1]))
{
$output .= "\n$" . $args[$i] . ' = ' . var_export($args[$i + 1], true) .
";";
$i++;
} else
if(isset($GLOBALS[$args[$i]]))
$output .= "\n$" . $args[$i] . ' = ' . var_export($GLOBALS[$args[$i]], true)
. ";";
$output .= "\n" .'?>';
fwrite($file,$output);
fclose($file);
chmod($path, 0777);
}