字符串函数
$url = 'C:\web\apache2.4\htdocs\cat.jpg';
$pos = strrpos($url, '\\');
// 截取文件名称,输出结果:cat.jpg
echo substr($url, $pos + 1);
// 截取文件所在的路径,输出结果:C:\web\apache2.4\htdocs
echo substr($url, 0, $pos);
$tel = '18810881888'; // 随意输入一串数字作为手机号
$len = 4; // 需要覆盖的手机号长度
$replace = str_repeat('*', $len); // 根据指定长度设置覆盖的字符串
echo substr_replace($tel, $replace, 3, $len); // 输出结果: 188****1888
str_repeat()函数用于对“*”字符重复$len次。
substr_replace()函数用于对字符串$tel中第3个位置开始后的$len长度的字符使用$replace进行替换。
字符串长度
$str = ‘PHP书籍';
echo strlen($str); // 输出结果:9 汉字占3
$str = ‘PHP书籍';
echo mb_strlen($str, 'UTF-8'); // 输出结果:5
echo ceil(5.2); // 输出结果:6
echo floor(7.8); // 输出结果:7
echo rand(1, 20); // 随机输出1到20间的整数
日期函数
echo time(); // 输出结果:1487666317
echo mktime(0, 0, 0, 2, 21, 2017); // 输出结果:1487606400
echo strtotime('2017-2-21'); // 输出结果:1487606400
echo microtime(); // 输出结果:0.04142600 1487666098
echo microtime(true); // 输出结果:1487666098.0414
echo date('Y-m-d H:i:s'); // 输出结果:2017-02-21 16:48:16
echo date('Y-m-d', 1487666317); // 输出结果:2017-02-21