Home Backend Development PHP Tutorial PHP utility function collection

PHP utility function collection

Jan 12, 2021 pm 03:56 PM
php

Recommended: "PHP Video Tutorial"

Practical Function Collection

<?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;
    }
}
Copy after login

The above is the detailed content of PHP utility function collection. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles