In-depth instructions for using PHP data cache _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
Http://www.jb51.net/article/23093.htm
function Set_cache ($name, $value) {
Set relative or absolute directories, don't add "/" at the end
$cache _dir = "./cache";
Setting extension
$cache _extension = ". php";

$cache _str_begin = " 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, check cache directory");
return false;
}
}

Converts an 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" = "a\\",
"B",
"C",
Array (
"C",
"D"
)
);

$fileAndVarName = "NewFile";

When generating $encode_str, in order to make the original character format unchanged in the string, the system compiles the predefined characters in the string before adding \ causes the predefined characters to remain in the string, but prints out the predefined characters only when outputting or printing the string, and does not print out the \ In front of the predefined characters.
$encode _str = Json_encode ($test _array);
Because this is to print the string into PHP code, when the output, the string of predefined characters will disrupt the program to run, so to the original escape character before the transfer of characters, so that the string output printed on the pre-defined characters before the escape character can also output
$addslashes _str = addslashes ($encode _str); Addslashes the pre-defined characters in a string so that they can be stored in a string that does not work, does not participate in the program run
Echo stripslashes ($addslashes _str); Reverses the literal function to remove 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 "
";


You can pass an array object or convert a JSON string into a JSON string that needs to be converted into a group.
Set_cache ("$fileAndVarName", $addslashes _str);
Var_dump ($addslashes _str);
echo "
";
Include_once "./cache/$fileAndVarName. php";
Var_dump ($ $fileAndVarName);
echo "
";

$decode _arr = (array) json_decode ($ $fileAndVarName);
Var_dump ($decode _arr);
echo "
";

Cache Another method, with serialize the array sequence number into a string, stored in any extension file, when used with fopen open read the string content, and then deserialized into the original data with Unserialize
$serialize _str = serialize ($test _array);
echo $serialize _str; This is the described array, but here is a string.
echo "
";
$unserialize _str = unserialize ($serialize _str); To restore the described data
Var_dump ($unserialize _str); Revert to $test _array, the array structure is not lost.
?>

http://www.bkjia.com/PHPjc/326975.html www.bkjia.com true http://www.bkjia.com/PHPjc/326975.html techarticle Copy the code as follows: PHP//http://www.jb51.net/article/23093.htm function Set_cache ($name, $value) {//Set relative or absolute directories, don't add "/" at the end Cache_dir = "....

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.