Copy codeThe Code is as follows:
<? Php
// Http://www.jb51.net/article/23093.htm
Function set_cache ($ name, $ value ){
// Set relative or absolute directories. Do not add "/" at the end "/"
$ Cache_dir = "./cache ";
// Set the 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;
// Cache 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;
}
}
// Convert array into a string from 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" => "\\",
"B ",
"C ",
Array (
"C ",
"D"
)
);
$ FileAndVarName = "newFile ";
// When $ encode_str is generated, in order to keep the original character format of the string unchanged, the system will add \ before the pre-defined characters in the string during compilation so that the pre-defined characters are kept in the string, however, when outputting or printing a string, only the predefined characters are printed, and \
$ Encode_str = json_encode ($ test_array );
// Print the string as PHP code. when outputting the string, the pre-defined characters in the string will disrupt the program running. Therefore, you must add the escape character before the original escape character, allow escape characters before pre-defined characters to be output during string output Printing
$ Addslashes_str = addslashes ($ encode_str); // addslashes adds \ before pre-defined characters in the string so that it can be stored in the string without any function.
Echo stripslashes ($ addslashes_str); // reverse yundun function, which can remove the backslash characters in the string. If there are two backslash lines in a row, remove one and leave one. If there is only one backslash, remove it directly.
Echo "<br> ";
// You can upload an array object or a json string to convert the object to a json string. You need to convert the object to an array.
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/> ";
// Cache another method. Use serialize to convert the array serial number into a string and store it in any extension file. Use fopen to open and read the string content, and then use unserialize to deserialize it into the original data.
$ Serialize_str = serialize ($ test_array );
Echo $ serialize_str; // This is the described array, but it is a string here.
Echo "<br/> ";
$ Unserialize_str = unserialize ($ serialize_str); // restore the data that has been described
Var_dump ($ unserialize_str); // restores it to $ test_array. The array structure is not lost.
?>