note 42500 added to function.var-export
| From: | Eugenephp at rn2 dot php dot net | Date: | Tue, 18 May 2004 22:58:43 +0000 |
| Subject: | note 42500 added to function.var-export | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69810@lists.php.net to get a copy of this message | ||
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);
}
----
Manual Page -- http://www.php.net/manual/en/function.var-export.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42500
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42500&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42500&report=yes
Search -- http://master.php.net/manage/user-notes.php