本文實例講述了php獲取指定數量隨機字符串的方法。分享給大家供大家參考,具體如下:
代碼如下復制代碼functiongetARandLetter($number= 1) {
if($number== 0)
returnFALSE;//去除0
$number=$number< 0 ? -$number:$number;//如果小於零取正值
$letterArr=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');
$returnStr=''
for($i= 0;$i<$number;$i++) {
$returnStr.=$letterArr[rand ( 0, 51 )];
}
return$returnStr;
}
echogetARandLetter(8);
運行結果:lUJfScvS