生成随机字符串 php

 1 /**
 2     +----------------------------------------------------------
 3     * 生成随机字符串
 4     +----------------------------------------------------------
 5     * @param int       $length  要生成的随机字符串长度
 6     * @param string    $type    随机码类型:0,数字+大小写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符
 7     +----------------------------------------------------------
 8     * @return string
 9     +----------------------------------------------------------
10     echo randCode(6,1);
11 */
12 function randCode($length = 32, $type = 0) {
13     $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
14     if ($type == 0) {
15         array_pop($arr);
16         $string = implode("", $arr);
17     } elseif ($type == "-1") {
18         $string = implode("", $arr);
19     } else {
20         $string = $arr[$type];
21     }
22     $count = strlen($string) - 1;
23     $code = '';
24     for ($i = 0; $i < $length; $i++) {
25         $code .= $string[rand(0, $count)];
26     }
27     return $code;
28 }

 

posted on 2020-06-01 19:06  白糖炒菜  阅读(203)  评论(0编辑  收藏  举报

导航