<?php
function getRandomCode ($length = 32, $mode = 0){
switch ($mode) {
case "1":
$str = "1234567890"; //纯数字
break;
case "2":
$str = "abcdefghijklmnopqrstuvwxyz"; //纯小写字母
break;
case "3":
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //纯大写字母
break;
case "4":
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //大小写字母混合
break;
case "5":
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //大写字母数字混合
break;
case "6":
$str = "abcdefghijklmnopqrstuvwxyz1234567890"; //小写字母数字混合
break;
default:
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; //大小写字母数字混合
break;
}
$result="";
$l=strlen($str);
for($i=0;$i < $length;$i++){
$num = rand(0, $l-1); //如果$l不减1,将不一定生成4位数, 因为$num = rand(0,10).会随机产生10,$str[10] 为空
$result .= $str[$num];
}
return $result;
}
?>
<?php
//调用输出
echo getRandomCode(4,1);
?>