<?php
Http://www.jb51.net/article/23093.htm
function Set_cache ($name, $value) {
Set a relative or absolute directory, and do not add "/" at the end
$cache _dir = "./cache";
Set extension
$cache _extension = ". php";
$cache _str_begin = "<?php\n//cache Created at:". Date ("Y-m-d h:i:s"). "\ n";
if (! Is_array ($value)) {
$cache _str_middle = "\$ $name = \" $value; ";
} else {
$cache _str_middle = "\$ $name =". Arrayeval ($value). ";";
}
$cache _str_end = "\n?>";
$cache _str = $cache _str_begin. $cache _str_middle. $cache _str_end;
Cached file path
$cache _file = "$cache _dir/$name $cache_extension";
if ($fp = @fopen ($cache _file, "WB")) {
Fwrite ($fp, $cache _str);
Fclose ($FP);
return true;
} else {
echo $cache _file;
Exit ("Can not write to cache files, please check cache directory");
return false;
}
}
Turn array into a string from the discuz!
function Arrayeval ($array, $level = 0) {
if (! Is_array ($array)) {
return "\" $array \ "";
}
$space = "";
for ($i = 0; $i <= $level; $i + +) {
$space. = "T";
}
$evaluate = "Array\n$space (\ n";
$comma = $space;
if (Is_array ($array)) {
foreach ($array as $key => $val) {
$key = is_string ($key)? "\"" . Addcslashes ($key, "\ \ \ \"). "\" ": $key;
$val =! Is_array ($val) && (! Preg_match ("/^\-?[ 1-9]\d*$/", $val) | | strlen ($val) > 12)? "\"" . Addcslashes ($val, "\ \ \ \"). "\" ": $val;
if (Is_array ($val)) {
$evaluate. = "$comma $key =>". Arrayeval ($val, $level + 1);
} else {
$evaluate. = "$comma $key => $val";
}
$comma = ", \n$space";
}
}
$evaluate. = "\n$space)";
return $evaluate;
}
$test _array = Array (
"6b" => "a\\",
"B",
"C",
Array (
"C",
"D"
)
);
$fileAndVarName = "NewFile";
When the $ENCODE_STR is generated, in order for the original character format in the string to remain unchanged, the system at compile time will give predefined characters in the string to keep the predefined characters in the string, but output or print the string will only print out the predefined characters, does not print out the predefined characters before the \
$encode _str = Json_encode ($test _array);
Because the string to be printed into PHP code, the output, the string of predefined characters will disrupt the program to run, so the original escape character before the transfer of characters, so that the string output printed in the predefined characters before the escape character can also output
$addslashes _str = addslashes ($encode _str); Addslashes a predefined character in a string so that it can be stored in a string does not work, does not participate in the program
Echo stripslashes ($addslashes _str); Reverses the function, which removes the backslash character from the string. If you have two consecutive backslashes, remove one and leave one. If there is only one backslash, remove it directly.
echo "<br>";
You can pass an array object, or you can pass it to a JSON string, convert it to a JSON string, and use it to convert a few more arrays
Set_cache ("$fileAndVarName", $addslashes _str);
Var_dump ($addslashes _str);
echo "<br/>";
Include_once "./cache/$fileAndVarName. php";
Var_dump ($ $fileAndVarName);
echo "<br/>";
$decode _arr = (array) json_decode ($ $fileAndVarName);
Var_dump ($decode _arr);
echo "<br/>";
Caching another method, using serialize to put the array sequence number into a string, stored in any extension file, use the fopen open read the string content, and then use Unserialize to deserialize the original data
$serialize _str = serialize ($test _array);
echo $serialize _str; This is the described array, but here's a string.
echo "<br/>";
$unserialize _str = unserialize ($serialize _str); Restore the data that you have described
Var_dump ($unserialize _str); The restore became $test _array, and the array structure was not lost.
?>