当前位置 博文首页 > 文章内容

    PHP实用函数集合

    作者:shunshunshun18 栏目:未分类 时间:2021-01-14 10:43:31

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    实用函数集合

    <?php
    
    if (!function_exists('number_random')) {
        /**
         * 生成随机数字串
         *
         * @param int $length
         * @return string
         */
        function number_random($length = 6)
        {
            $result = '';
            for ($i = 0; $i < $length; $i++) {
                $result .= mt_rand(0, 9);
            }
    
            return $result;
        }
    }
    
    if (!function_exists('string_random')) {
        /**
         * 生成随机字符串
         *
         * @param int $length
         * @return string
         */
        function string_random($length = 6)
        {
            $result = '';
            for ($i = 0; $i < $length; $i++) {
                $rand = mt_rand(1, 3);
                switch ($rand) {
                    case 1:
                        $result .= mt_rand(0, 9);
                        break;
                    case 2:
                        $result .= chr(mt_rand(65, 90));
                        break;
                    default:
                        $result .= chr(mt_rand(97, 122));
                        break;
                }
            }
    
            return $result;
        }
    }
    
    if (!function_exists('get_order_number')) {
        /**
         * 生成订单号
         *
         * @param int $length
         * @return string
         */
        function get_order_number($length = 32)
        {
            $date = date('YmdHis');
            $micro = explode('.', microtime(true))[1];
            $rand = string_random($length - (strlen($date) + strlen($micro)));
    
            return $date . $micro . $rand;
        }
    }
    
    if (!function_exists('check_bank_card')) {
        /**
         * 验证银行卡号
         *
         * @param string $card
         * @return bool
         */
        function check_bank_card(string $card)
        {
            $arr_no = str_split($card);
            $last_n = $arr_no[count($arr_no) - 1];
            krsort($arr_no);
            $i = 1;
            $total = 0;
            foreach ($arr_no as $n) {
                if ($i % 2 == 0) {
                    $ix = $n * 2;
                    if ($ix >= 10) {
                        $nx = 1 + ($ix % 10);
                        $total += $nx;
                    } else {
                        $total += $ix;
                    }
                } else {
                    $total += $n;
                }
                $i++;
            }
            $total -= $last_n;
            $total *= 9;
    
            return $last_n == ($total % 10);
        }
    }
    
    if (!function_exists('blocking_lock')) {
        /**
         * 阻塞锁
         *
         * @param string $lock_name 锁名字
         * @param int $valid 有效秒数
         * @return mixed
         */
        function blocking_lock(string $lock_name, $valid = 3600)
        {
            $lock_key = 'blocking_lock';
            while ($exp = Redis::hget($lock_key, $lock_name)) {
                if ($exp < microtime(true)) {
                    Redis::hdel($lock_key, $lock_name);
                }
                usleep(10);
            }
    
            return Redis::hset($lock_key, $lock_name, microtime(true) + $valid);
        }
    }
    
    if (!function_exists('blocking_unlock')) {
        /**
         * 释放阻塞锁
         *
         * @param string $lock_name
         * @return mixed
         */
        function blocking_unlock(string $lock_name)
        {
            $lock_key = 'blocking_lock';
    
            return Redis::hdel($lock_key, $lock_name);
        }
    }
    
    if (!function_exists('random_color')) {
        /**
         * 随机十六进制颜色
         *
         * @return string
         */
        function random_color()
        {
            $str = '#';
            for ($i = 0; $i < 6; $i++) {
                $randNum = rand(0, 15);
                switch ($randNum) {
                    case 10:
                        $randNum = 'a';
                        break;
                    case 11:
                        $randNum = 'b';
                        break;
                    case 12:
                        $randNum = 'c';
                        break;
                    case 13:
                        $randNum = 'd';
                        break;
                    case 14:
                        $randNum = 'e';
                        break;
                    case 15:
                        $randNum = 'f';
                        break;
                }
                $str .= $randNum;
            }
    
            return $str;
        }
    }
    
    if (!function_exists('get_hour_history')) {
        /**
         * 获取当日历史小时
         *
         * @return array
         */
        function get_hour_history()
        {
            $history = [];
            for ($i = 0; $i <= date('H'); $i++) {
                $history[] = $i;
            }
    
            return $history;
        }
    }