本文实例讲述了PHP获取指定数量随机字符串的方法。分享给大家供大家参考,具体如下:
PHP;">
function getARandLetter($number = 1) {
if ($number == 0)
return FALSE; //去除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;
}
echo getARandLetter(8);
运行结果:lUJfScvS
PS:这里再为大家提供一款功能类似的在线工具供大家参考:
高强度密码生成器: