// http://www.jb51.net/article/23093.htm
function set_cache($name, $value) {
// 设置相对或者绝对目录,末尾不要加 "/"
$cache_dir = "./cache";
// 设置扩展名
$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 = "$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 $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";
// 在生成$encode_str的时候,为使字符串中原有字符格式不变,系统在编译时会给字符串中预定义字符前加 \ 使预定义字符保留在字符串中,但输出或打印字符串的时候只会输出打印出预定义字符,不会打印出预定义字符前面的 \
$encode_str = json_encode ( $test_array );
// 因为这里要把字符串打印成PHP代码,输出的时候,字符串中预定义字符会打乱程序运行,所以要在原有转义字符前再加转移字符,使字符串输出打印时在预定义字符前转义字符也能输出
$addslashes_str = addslashes ( $encode_str ); // addslashes将字符串中预定义字符前加 \ 使其能存放在字符串中不产生作用,不参与程序运行
echo stripslashes($addslashes_str); // 反转义函数,可去掉字符串中的反斜线字符。若是连续二个反斜线,则去掉一个,留下一个。若只有一个反斜线,就直接去掉。
echo "
";
// You can pass an array object or a string converted into json, which needs to be converted into an array when used.
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 "
";
// Another method of caching, use serialize to convert the array serial number into a string, store it in a file with any extension, use fopen to open it to 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 array described but here it is just a string
echo "
";
$unserialize_str = unserialize ( $serialize_str ); // Restore the described data
var_dump($unserialize_str); // Restore to $test_array, the array structure is not lost.
?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

转化方法:1、使用“mb_substr($url,stripos($url,"?")+1)”获取url的参数部分;2、使用“parse_str("参数部分",$arr)”将参数解析到变量中,并传入指定数组中,变量名转为键名,变量值转为键值。

在php中,可以利用ltrim()函数来去掉字符串首位的tab空白符,语法为“ltrim(string)”;当只给ltrim()函数传入一个参数,用于规定要检查的字符串时,可删除该字符串开始位置的空白字符(例空格、tab制表符、换行符等)。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
