zl程序教程

您现在的位置是:首页 >  后端

当前栏目

php生成随机数或者字符串的代码

PHP代码 字符串 生成 随机数 或者
2023-06-13 09:14:02 时间
$len表示长度,代码如下:
复制代码代码如下:

/**
*产生随机字符串
*
*产生一个指定长度的随机字符串,并返回给用户
*
*@accesspublic
*@paramint$len产生字符串的位数
*@returnstring
*/
functionrandstr($len=6){
$chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789-@#~";
//characterstobuildthepasswordfrom
mt_srand((double)microtime()*1000000*getmypid());
//seedtherandomnumbergenerater(mustbedone)
$password="";
while(strlen($password)<$len)
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return$password;
}