PHP实用函数集合

实用函数集合

<?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;
    }
}

关于PHP实用函数集合的文章就分享到这,如果对你有帮助欢迎继续关注我们哦

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/44689.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
php学习php学习订阅用户
上一篇 2022年6月27日 00:30
下一篇 2022年6月27日 00:31

相关推荐

  • PHP中如何进行前端框架和后端框架的集成?

    随着Web应用程序开发的日益复杂和需要的交互性越来越高,使用前端框架和后端框架已经变得非常普遍。在此过程中,集成前端框架和后端框架也成为必不可少的步骤,以确保应用程序的顺畅运行和高效性能。本文将重点介绍…

    2023年5月17日
    00
  • 通过PHP内置web服务器实现简单的调试应用

    # 在自己家目录下创建www目录 [root@localhost ~]# mkdir www [root@localhost ~]# cd www/ # 创建几个php脚本用于测试 index.php info.php # 启动一个Web服务器 [root@localhost www]# php -S 192.168.204.151:800…

    2022年6月25日 PHP自学教程
    0167
  • 用PHP屏蔽关键字,敏感词,你用哪些方法(附代码)

    在文章评论,分享内容中有时候会遇到屏蔽敏感词,关键字等之类的。本文介绍了PHP屏蔽关键字实现方法,一共有两种实现方式,具体如下: 第一种方法 思路 用正则去匹配关键字,把关键字用别的字符替换 $str = "/你大…

    2018年8月27日
    0373
  • PHP入门指南:解释器模式。

    随着计算机技术的不断发展,编程语言的种类也越来越多。其中一门被广泛应用于Web开发的语言是PHP。PHP是一种解释型语言,具有广泛的应用领域和良好的跨平台性。本文将为初学者介绍PHP解释器模式。一、什么是解释器…

    2023年5月23日
    06
  • 我来教你美国php虚拟主机购买怎么用。

    在互联网时代,网站已经成为企业和个人展示形象、传播信息的重要途径。为了更好地搭建和维护网站,越来越多的人选择购买海外php云虚拟主机。以下是购买和使用美国PHP虚拟主机的一些建议:,,1. **选择合适的虚拟主…

    2024年7月8日
    00
  • PHP8中的函数:str_begins_with()的多种使用场景。

    随着PHP编程语言的不断发展,其语法和函数库也在不断地更新和完善。PHP8中新增加的函数str_begins_with()提供了一种新的方式来判断字符串是否以指定的前缀开头。本文将介绍str_begins_with()函数的多种使用场景,帮…

    2023年5月21日
    00
  • PHP中的项目实施。

    随着互联网、移动互联网的快速发展,越来越多的企业和个人都开始关注开发自己的网站、应用程序和移动端应用等。而其中一门开发语言PHP也逐渐成为了开发者们的选择,由于其开发成本低、使用便捷、跨平台、跨数据库等…

    2023年5月28日
    00
  • PHP 管理全局的方法

    【相关学习推荐:php编程(视频)】管理全局状态在命令式语言中总是需要一些全局空间。在编程 PHP 或扩展时,我们将明确区分我们所称的请求绑定全局变量和真正的全局变量。请求全局变量是处理请求过程中需要携带和…

    2022年6月19日
    0119

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息