php内置函数

关于php函数的一些学习点滴

字符串函数

// 获取字符串长度
$str = 'My name is neo';
echo strlen($str);

// 去除字符左右两端空格
$str = '  abcd  ';
echo trim($str);

// 首字母大写
$str = 'world';
echo ucfirst($str);
$str = 'hello world';
echo ucwords($str);

// md5转换
$str = 'dning';
echo md5($str);

// 字符串切割
$str = '1234.abc.ok';
print_r(explode('.', $str));

// 字符串截取
$str = 'dning.txt';
echo substr($str, 6);

// 查找字符串中某个子串 首次出现位置 (若没有返回false)
$str = 'abcdab';
var_dump(strpos($str, 'g'));

数组函数

// =============数组定义
$arr = ['dany', 'neo'];
$arr = [1, 1, 2, 3, 4];
print_r($arr);

// =============往数组里追加一个元素
 array_push($arr, 'dning');
 print_r($arr);

// =============删除数组最后一个元素
array_pop($arr);
print_r($arr);

// =============从数组的开头 添加一个元素
array_unshift($arr, 'ding');
print_r($arr);

// =============删除数组第一个元素
array_shift($arr);
print_r($arr);

// =============count() 数组长度
echo count($arr);

// =============检测普通数组中是否含有某个元素
$arr = ['dning', 'dany', 'neo'];
if (in_array('dd', $arr)) {
    echo 'ok';
}else {
    echo 'error';
}

// =============检测关联数组中是否含有 某个键
$arr = [
    'dning' => 1,
    'dany' => 2,
    'neo' => 3
];
if (array_key_exists('dning', $arr)) {
    echo 'ok';
}else {
    echo 'error';
}

// =============获取关联数组的所有键
$arr = [
    'name' => 'dany',
    'gender' => 'man',
    'age' => 21
];
$rs = array_keys($arr);
print_r($rs);

// =============数组过滤
$users = [
    ['name' => 'dning', 'age' => 21],
    ['name' => 'dany', 'age' => 20],
    ['name' => 'neo', 'age' => 19],
];
// $rs = array_filter($users, function ($item) {
//     return $item['age'] > 20;
// });
$rs = array_filter($users, function($item) {
    return in_array($item['name'], ['dning', 'neo']);
});
print_r($rs);

// =============操作数组的每一项 map函数
$arr = [1, 2, 3, 4, 5, 6, 7];
$mapArr =  array_map(function($item){
    return $item + 1;
}, $arr);
print_r($mapArr);
$arr = [
    ['name' => 'dning'],
    ['name' => 'dany'],
    ['name' => 'neo'],
    ['name' => 'wang'],
];
$mapArr = array_map(function ($item) {
    $item['school'] = '学院';
    return $item;
}, $arr);
print_r($mapArr);

// =============获取关联数组的值
$arr = [
    ['name' => 'dning', 'age' => 21, 'gender' => 'man'],
    ['name' => 'dany', 'age' => 20, 'gender' => 'man'],
    ['name' => 'neo', 'age' => 18, 'gender' => 'man'],
];
$rs = implode('-', array_values($arr[1]));
echo $rs;

// =============数组合并
$arr1 = [1, 2, 3, 4];
$arr2 = [1, 1];
$rs = array_merge($arr1, $arr2);
print_r($rs);
// 关联数组合并时,如果存在同名键,后边的会覆盖前边的
$arr1 = [
    'name' => 'dning',
    'gender' => 'man',
    'age' => 21,
];
$arr2 = [
    'name' => 'neo',
    'class' => '全栈34班',
];
$rs = array_merge($arr1, $arr2);
print_r($rs);

数组递归统计

假定某个国家 有如下等级划分:酋长 - 将军 - 领主,且他们各自拥有部队troops,求该国家共有多少军队

// =============递归统计一个国家的兵力
// 酋长 - 将军 - 领主
$country = [
    [
        'name' => '酋长1',
        'troops' => '1000',
        'children' => [
            [
                'name' => '将军1',
                'troops' => '600',
                'children' => [
                    [
                        'name' => '领主1-1',
                        'troops' => '200',
                    ],
                    [
                        'name' => '领主1-2',
                        'troops' => '250',
                    ],
                ],
            ],
            [
                'name' => '将军2',
                'troops' => '600',
                'children' => [
                    [
                        'name' => '领主2-1',
                        'troops' => '400',
                    ],
                ],
            ],
            [
                'name' => '将军3',
                'troops' => '600',
            ],
        ],
    ],
    [
        'name' => '酋长2',
        'troops' => '1200',
    ],
];

$arr = [];
function unFold($children, &$arr)
{
   $arr = [...$arr, ...$children];
    foreach ($children as $item) {
        if (array_key_exists('children', $item)) {
            unFold($item['children'], $arr);
        }
    }
}
unFold($country, $arr);
echo array_sum(array_column($arr,'troops'));

序列化与反序列化

// =============使用序列化和反序列化 持久保存数据
$data = [
    'name' => 'dning',
    'gender' => 'man',
    'age' => '21',
    'hobby' => ['足球', '篮球', '象棋', '编程'],
];
function cache($name, $data = null)
{
    $file = './cache/' . md5($name) . '.php';
    if (is_null($data)) {
        // 读取
        $content = is_file($file) ? file_get_contents($file) : null;
        return $content ? unserialize($content) : null;
    } else {
        // 写入
        file_put_contents($file, serialize($data));
    }
}
cache('db', $data);
$rs = cache('db');
print_r($rs);

时间函数

// =============时间操作
//1.时间戳
echo time();
echo microtime(true);
//2.当前时间
echo date('Y-m-d H:i:s');
//3.日期格式 和时间戳格式 互相转换
echo date('Y-m-d H:i:s',time() - 3600);
echo strtotime('2021-12-10');

数学函数

// =============数学函数
//向上取整
echo ceil(1.2);
//向下取整
echo floor(1.9);
//求最大值
echo max(1,2);
//求最小值
echo min(1, 2);
//四舍五入
echo round(1.1);

// eg: 随机验证码
function validCode($len) {
    $str = '123456789abcdefghijklnop';
    $code = '';
    for ($i = 0; $i < $len; $i++) {
        $index = floor(mt_rand(0, strlen($str) -1));
        $code .= $str[$index];
    }
    return strtoupper($code);
}
echo validCode(5);

正则操作

//正则匹配
// eg:手机号匹配
$rs = preg_match('/^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/', '1337002483');
// $rs为1 表示字符串合法,$rs为0 表示不合法
echo $rs;

//正则切割
// eg:有字符串 1@2#3%4,请按照 @ # % 做切割
$str = '1@2#3%4';
echo $str . '<br/>';
$newStr = implode('',preg_split('/@|#|%/', $str));
print_r($newStr);

//正则替换
// eg:有字符串 1@2#3%4,请将字符串中 @ # % 去掉
$str = '1@2#3%4';
echo $str . '<br/>';
$newStr = preg_replace('/@|#|%/', '', $str);
echo $newStr;

文件操作

// 1.写入文件
$rs = file_put_contents('dany.txt', 'ok ok ok ~~~');
// 成功后会返回 该文件大小
echo $rs;

// 2.读取远程文件(也支持远程文件 )
$rs = file_get_contents('dany.txt');
echo $rs;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值