PHP实用函数集合

 3279

实用函数集合,常用于各类数据验证、订单号和随机字符生成等场景使用。都是比较实用的,还是比较可以的~~

  1. <?php 
  2. if (!function_exists('number_random')) {
  3.     /**
  4.      * 生成随机数字串
  5.      *
  6.      * @param int $length
  7.      * @return string
  8.      */
  9.     function number_random($length = 6)
  10.     {
  11.         $result = '';
  12.         for ($i = 0; $i < $length; $i++) {
  13.             $result .= mt_rand(0, 9);
  14.         }
  15.         return $result;
  16.     }
  17. } 
  18. if (!function_exists('string_random')) {
  19.     /**
  20.      * 生成随机字符串
  21.      *
  22.      * @param int $length
  23.      * @return string
  24.      */
  25.     function string_random($length = 6)
  26.     {
  27.         $result = '';
  28.         for ($i = 0; $i < $length; $i++) {
  29.             $rand = mt_rand(1, 3);
  30.             switch ($rand) {
  31.                 case 1:
  32.                     $result .= mt_rand(0, 9);
  33.                     break;
  34.                 case 2:
  35.                     $result .= chr(mt_rand(65, 90));
  36.                     break;
  37.                 default:
  38.                     $result .= chr(mt_rand(97, 122));
  39.                     break;
  40.             }
  41.         }
  42.         return $result;
  43.     }
  44. } 
  45. if (!function_exists('get_order_number')) {
  46.     /**
  47.      * 生成订单号
  48.      *
  49.      * @param int $length
  50.      * @return string
  51.      */
  52.     function get_order_number($length = 32)
  53.     {
  54.         $date = date('YmdHis');
  55.         $micro = explode('.', microtime(true))[1];
  56.         $rand = string_random($length - (strlen($date) + strlen($micro)));
  57.         return $date . $micro . $rand;
  58.     }
  59. } 
  60. if (!function_exists('check_bank_card')) {
  61.     /**
  62.      * 验证银行卡号
  63.      *
  64.      * @param string $card
  65.      * @return bool
  66.      */
  67.     function check_bank_card(string $card)
  68.     {
  69.         $arr_no = str_split($card);
  70.         $last_n = $arr_no[count($arr_no) - 1];
  71.         krsort($arr_no);
  72.         $i = 1;
  73.         $total = 0;
  74.         foreach ($arr_no as $n) {
  75.             if ($i % 2 == 0) {
  76.                 $ix = $n * 2;
  77.                 if ($ix >= 10) {
  78.                     $nx = 1 + ($ix % 10);
  79.                     $total += $nx;
  80.                 } else {
  81.                     $total += $ix;
  82.                 }
  83.             } else {
  84.                 $total += $n;
  85.             }
  86.             $i++;
  87.         }
  88.         $total -= $last_n;
  89.         $total *= 9;
  90.  
  91.         return $last_n == ($total % 10);
  92.     }
  93. }
  94. if (!function_exists('blocking_lock')) {
  95.     /**
  96.      * 阻塞锁
  97.      *
  98.      * @param string $lock_name 锁名字
  99.      * @param int $valid 有效秒数
  100.      * @return mixed
  101.      */
  102.     function blocking_lock(string $lock_name, $valid = 3600)
  103.     {
  104.         $lock_key = 'blocking_lock';
  105.         while ($exp = Redis::hget($lock_key, $lock_name)) {
  106.             if ($exp < microtime(true)) {
  107.                 Redis::hdel($lock_key, $lock_name);
  108.             }
  109.             usleep(10);
  110.         }
  111.         return Redis::hset($lock_key, $lock_name, microtime(true) + $valid);
  112.     }
  113. } 
  114. if (!function_exists('blocking_unlock')) {
  115.     /**
  116.      * 释放阻塞锁
  117.      *
  118.      * @param string $lock_name
  119.      * @return mixed
  120.      */
  121.     function blocking_unlock(string $lock_name)
  122.     {
  123.         $lock_key = 'blocking_lock';
  124.         return Redis::hdel($lock_key, $lock_name);
  125.     }
  126. } 
  127. if (!function_exists('random_color')) {
  128.     /**
  129.      * 随机十六进制颜色
  130.      *
  131.      * @return string
  132.      */
  133.     function random_color()
  134.     {
  135.         $str = '#';
  136.         for ($i = 0; $i < 6; $i++) {
  137.             $randNum = rand(0, 15);
  138.             switch ($randNum) {
  139.                 case 10:
  140.                     $randNum = 'a';
  141.                     break;
  142.                 case 11:
  143.                     $randNum = 'b';
  144.                     break;
  145.                 case 12:
  146.                     $randNum = 'c';
  147.                     break;
  148.                 case 13:
  149.                     $randNum = 'd';
  150.                     break;
  151.                 case 14:
  152.                     $randNum = 'e';
  153.                     break;
  154.                 case 15:
  155.                     $randNum = 'f';
  156.                     break;
  157.             }
  158.             $str .= $randNum;
  159.         }
  160.         return $str;
  161.     }
  162. } 
  163. if (!function_exists('get_hour_history')) {
  164.     /**
  165.      * 获取当日历史小时
  166.      *
  167.      * @return array
  168.      */
  169.     function get_hour_history()
  170.     {
  171.         $history = [];
  172.         for ($i = 0; $i <= date('H'); $i++) {
  173.             $history[] = $i;
  174.         }
  175.         return $history;
  176.     }
  177. }



TAG标签:
本文网址:https://www.zztuku.com/detail-8606.html
站长图库 - PHP实用函数集合
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐