目录
strtoupper — 字符串转大写
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; // 打印 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
strtolower — 字符串转小写
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // 打印 mary had a little lamb and she loved it so
strpos — 查找字符串首次出现的位置
$big_str = 'abc';
$find_str = 'a';
//找不到返回 false,找到就返回首次出现的位置(第一个位置是0)
$pos = strpos($big_str, $find_str);
// 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,
// 因为 'a' 是第 0 位置上的(第一个)字符。
if ($pos === false) {
echo "没找到";
} else {
echo "找到了";
}
is_numeric — 检测变量是否为数字或数字字符串
is_numeric(12.); // true
is_numeric(-12.2); // true
is_numeric("-124.3"); // true
is_numeric("-124."); // true
is_numeric("0.8"); // true
is_numeric(0); // true
is_numeric("0"); // true
is_numeric(NULL); // false
is_numeric(true); // false
is_numeric(false); // false
is_numeric("324jdas32"); // false
is_numeric("123-"); // false
is_numeric(1e7); // true
is_numeric("1e7"); // true
is_numeric(0x155); // true
is_numeric("0x155"); // false
sprintf — 格式化字符串
// 保留一位小数
sprintf('%.1f', '1.23');// "1.2"
sprintf('%.1f', '1.26');// "1.3"
sprintf('%.1f', '.33');// "0.3"
// 保留 0位小数
sprintf('%.0f', '1.23');// "1"
// 整数部分保留4位,不足补零
sprintf('%04d', '3');// "0003"
sprintf('%04d', '12345');// "12345"
sprintf('%04d', '13.88');// "0013"
explode — 字符串 转 数组
$str = "a,b,c,d";
$arr = explode(',', $str);
implode — 数组 转 字符串
$arr = array('a', 'b', 'c');
$str = implode(",", $arr);
echo $str; // "a,b,c"
in_array — 检查数组中是否存在某个值
如果找到则返回 TRUE,否则返回 FALSE
注意:
- 如果 needle 是字符串,则比较是区分大小写的。
- 第三个参数默认为false,表示比较时值相同即可,类型可以不同;
如果第三个参数的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。
$arr = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $arr)) {
echo "Got Irix";
}
str_replace — 字符串替换
$big_str = 'hello world';
$find_str = 'o';
$res = str_replace($find_str, '', $big_str);
echo $res; //"hell wrld"
json_decode — json串 转 数组
json_decode($json_str, true);
json_encode — 数组 转 json串
json_encode($arr);//数组转json串
date — 日期时间格式化
$res = date("Y-m-d H:i:s", time());
echo $res;// "2020-08-19 15:22:02"