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购物车类Cart.class.php定义与用法

    本文实例讲述了PHP购物车类Cart.class.php定义与用法。分享给大家供大家参考,具体如下: 之前的开发人员使用了JS的技术开发了一套前台购物车(删除添加什么的都使用JS),但是浏览器兼容不好, 今天终于出问题了, 有个…

    2022年6月11日
    0128
  • PHP8.0中的SSH连接库:phpseclib

    小编前言:本篇文章主要介绍PHP8.0中的SSH连接库phpseclib,旨在帮助PHP开发者更加深入了解和应用这个工具。 随着互联网技术的不断发展,远程操作服务器成为了越来越多开发者必须面对的问题。其中,SSH连接是一种非…

    2023年5月18日
    04
  • 详解PHP中的输出缓冲控制(Output Control)

    本篇文章带大家了解一下PHP中的输出缓冲控制(Output Control) 。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 在 PHP 中,我们直接进行 echo 、 或者 print_r 的时候,输出的内容就会直接打…

    2023年3月29日
    00
  • 小编教你php 建站模板。

    “快速搭建PHP网站,提供高质量建站模板。” 建站模板为什么都是PHP? 在互联网高速发展的今天,网站已经成为了企业、个人展示自己的重要平台,而建站模板则是网站建设过程中不可或缺的一部分,为什么建…

    2024年7月25日
    00
  • PHP8函数:get_debug_type()的用途详解。

    PHP8是PHP语言的最新版本。这个版本新增了很多强大的功能和改进,在解释器性能、类型系统和错误处理方面有很多进展。其中一个非常实用的新功能是get_debug_type()函数。它可以方便地获取表达式的类型信息,这个函数…

    2023年5月21日
    01
  • PHP商城开发中的页面性能优化

    随着电商市场的逐步扩大,越来越多的企业选择建立自己的电商平台来开展业务。而在建立电商平台的过程中,开发人员需要考虑的不仅是平台的功能,还需要考虑页面的性能优化问题,以提高用户体验和网站的排名。本文将…

    2023年5月19日
    01
  • PHP入门指南:Zend框架。

    PHP入门指南:Zend框架 PHP是一种流行的开源脚本语言,它被广泛应用于Web应用程序的开发。然而,PHP的开发者往往需要使用框架来提高开发效率和代码复用性。Zend框架是一个流行的PHP框架,它的设计思想是为了提高代…

    2023年5月23日
    04
  • PHP入门指南:命令模式。

    PHP入门指南:命令模式 命令模式是一种行为设计模式,它允许您将操作封装为对象。 在这种模式中,命令实现者将一系列参数传递给命令接收者并触发执行。 在本文中,我们将介绍PHP命令模式的基础知识和实例。 命令模…

    2023年5月22日
    00

联系我们

QQ:951076433

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