Rumah > pembangunan bahagian belakang > tutorial php > PHP实用函数集合

PHP实用函数集合

藏色散人
Lepaskan: 2023-04-09 17:38:01
ke hadapan
3821 orang telah melayarinya

推荐:《PHP视频教程

实用函数集合

<?php

if (!function_exists(&#39;number_random&#39;)) {
    /**
     * 生成随机数字串
     *
     * @param int $length
     * @return string
     */
    function number_random($length = 6)
    {
        $result = &#39;&#39;;
        for ($i = 0; $i < $length; $i++) {
            $result .= mt_rand(0, 9);
        }

        return $result;
    }
}

if (!function_exists(&#39;string_random&#39;)) {
    /**
     * 生成随机字符串
     *
     * @param int $length
     * @return string
     */
    function string_random($length = 6)
    {
        $result = &#39;&#39;;
        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(&#39;get_order_number&#39;)) {
    /**
     * 生成订单号
     *
     * @param int $length
     * @return string
     */
    function get_order_number($length = 32)
    {
        $date = date(&#39;YmdHis&#39;);
        $micro = explode(&#39;.&#39;, microtime(true))[1];
        $rand = string_random($length - (strlen($date) + strlen($micro)));

        return $date . $micro . $rand;
    }
}

if (!function_exists(&#39;check_bank_card&#39;)) {
    /**
     * 验证银行卡号
     *
     * @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(&#39;blocking_lock&#39;)) {
    /**
     * 阻塞锁
     *
     * @param string $lock_name 锁名字
     * @param int $valid 有效秒数
     * @return mixed
     */
    function blocking_lock(string $lock_name, $valid = 3600)
    {
        $lock_key = &#39;blocking_lock&#39;;
        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(&#39;blocking_unlock&#39;)) {
    /**
     * 释放阻塞锁
     *
     * @param string $lock_name
     * @return mixed
     */
    function blocking_unlock(string $lock_name)
    {
        $lock_key = &#39;blocking_lock&#39;;

        return Redis::hdel($lock_key, $lock_name);
    }
}

if (!function_exists(&#39;random_color&#39;)) {
    /**
     * 随机十六进制颜色
     *
     * @return string
     */
    function random_color()
    {
        $str = &#39;#&#39;;
        for ($i = 0; $i < 6; $i++) {
            $randNum = rand(0, 15);
            switch ($randNum) {
                case 10:
                    $randNum = &#39;a&#39;;
                    break;
                case 11:
                    $randNum = &#39;b&#39;;
                    break;
                case 12:
                    $randNum = &#39;c&#39;;
                    break;
                case 13:
                    $randNum = &#39;d&#39;;
                    break;
                case 14:
                    $randNum = &#39;e&#39;;
                    break;
                case 15:
                    $randNum = &#39;f&#39;;
                    break;
            }
            $str .= $randNum;
        }

        return $str;
    }
}

if (!function_exists(&#39;get_hour_history&#39;)) {
    /**
     * 获取当日历史小时
     *
     * @return array
     */
    function get_hour_history()
    {
        $history = [];
        for ($i = 0; $i <= date(&#39;H&#39;); $i++) {
            $history[] = $i;
        }

        return $history;
    }
}
Salin selepas log masuk

Atas ialah kandungan terperinci PHP实用函数集合. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
php
sumber:learnku.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan