- /**
- * 生成随机数
- *
- * @param int $length 生成字符串长度
- * @param int $type 字符串类型
- * @param bool $special 是否使用特殊字符
- * @return string 返回生成的随机字符串
- * @example random(10, null, true);
- */
- function random($length, $type = NULL, $special = FALSE) {
- $str = "";
- switch ($type) {
- case 1:
- $str = "0123456789";
- break;
- case 2:
- $str = "abcdefghijklmnopqrstuvwxyz";
- break;
- case 3:
- $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- break;
- case 4:
- $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- break;
- default:
- $str = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
- break;
- }
- return substr(str_shuffle(($special != FALSE) ? '!@#$%^&*()_+' . $str : $str), 0, $length);
- }
复制代码
|