php获取随机字符串的几种方法
方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) 1 2 3 4…
方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* 获得随机字符串
* @param $len 需要的长度
* @param $special 是否需要特殊符号
* @return string 返回随机字符串
*/
function getRandomStr($len, $special=true){
$chars = array(
“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”,
“l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”,
“w”, “x”, “y”, “z”, “A”, “B”, “C”, “D”, “E”, “F”, “G”,
“H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”,
“S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”, “0”, “1”, “2”,
“3”, “4”, “5”, “6”, “7”, “8”, “9”
);
if($special){
$chars = array_merge($chars, array(
“!”, “@”, “#”, “$”, “?”, “|”, “{“, “/”, “:”, “;”,
“%”, “^”, “&”, “*”, “(“, “)”, “-“, “_”, “[“, “]”,
“}”, “<“, “>”, “~”, “+”, “=”, “,”, “.”
));
}
$charsLen = count($chars) – 1;
shuffle($chars); //打乱数组顺序
$str = ”;
for($i=0; $i<$len; $i++){
$str .= $chars[mt_rand(0, $charsLen)]; //随机取出一位
}
return $str;
}
|
方法二、str_shuffle函数(打乱字符串顺序)和mt_rand函数
1
2
3
4
|
//取随机10位字符串
$strs=“QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm”;
$name=substr(str_shuffle($strs),mt_rand(0,strlen($strs)–11),10);
echo $name;
|
方法三、md5(),uniqid(),microtime()生成唯一的32位字符串
1
2
|
$uniqid = md5(uniqid(microtime(true),true));
//microtime(true) 返回系统当前时间戳的毫秒数
|
第一种:mt_rand()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
function GetRandStr($length){
//字符组合
$str = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789’;
$len = strlen($str)–1;
$randstr = ”;
for ($i=0;$i<$length;$i++) {
$num=mt_rand(0,$len);
$randstr .= $str[$num];
}
return $randstr;
}
//传入长度
$number = GetRandStr(6);
echo $number;
?>
|
第二种:array_rand()数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
function make_password($length)
{
// 密码字符集,可任意添加你需要的字符
$str = array(‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’,
‘i’, ‘j’, ‘k’, ‘l’,‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’,
‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’,‘z’, ‘A’, ‘B’, ‘C’, ‘D’,
‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’,‘M’, ‘N’, ‘O’,
‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’,‘Z’,
‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’);
// 在 $str 中随机取 $length 个数组元素键名
$keys = array_rand($str, $length);
$password = ”;
for($i = 0; $i < $length; $i++)
{
// 将 $length 个数组元素连接成字符串
$password .= $str[$keys[$i]];
}
return $password;
}
echo make_password(6);
?>
|
第三种:把字符串打乱,然后返回其中的一小截
1
2
3
4
5
6
7
8
9
|
<?php
function getrandstr($length){
$str = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890’;
$randStr = str_shuffle($str);//打乱字符串
$rands= substr($randStr,0,$length);//substr(string,start,length);返回字符串的一部分
return $rands;
}
echo getrandstr(6);
?>
|
第四种:返回任意随机数
1
2
3
4
|
<?php
//返回1000-9999其中的一个随机数
echo rand(1000,9999);
?>
|
第五种:对时间戳进行MD5加密,截取其中一部分
1
2
3
4
5
6
7
8
|
<?php
function token($length){
$str = md5(time());
$token = substr($str,5,$length);
return $token;
}
echo token(6);
?>
|
https://www.jb51.net/article/187452.htm
类别:PHP 技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!