本文整理自网络,侵删。
<?php
// http://1000zx.cn/article/23093.htm
function set_cache($name, $value) {
// 设置相对或者绝对目录,末尾不要加 "/"
$cache_dir = "./cache";
// 设置扩展名
$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 = "$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;
}
}
// 将array变成字符串, 来自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;
}
相关阅读 >>
更多相关阅读请进入《Discuz论坛》频道 >>
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。