php产生随机字符串
解决方法:
$length字符串长度
$type生成字符串类型,0(默认):26字母大小写+数字,1:数字,2:26字母小写,3:26字母大写,4:26字母大小写,5:字母小写+数字,6:字母大写+数字
$length = 5; $type = 0;$patten ='';$result='';
if (!$patten) {
switch ($type) {
case 1:
$patten = '0123456789';
break;
case 2:
$patten = 'abcdefghigklmnopqrstuvwxyz';
break;
case 3:
$patten = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 4:
$patten = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz';
break;
case 5:
$patten = 'abcdefghigklmnopqrstuvwxyz0123456789';
break;
case 6:
$patten = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
break;
default:
$patten = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghigkmnpqrstuvwxyz0123456789';
break;
}
}
for($i=1;$i>0;$i++){
if(strlen($result)<$length){
$rand=rand(0,strlen($patten));
$result.= substr($patten, $rand, 1);
}else{
break;
}
}
echo $result;