那么我们知道在PHP中简单的四位数的纯数字验证可以用rand(1000,9999)就可以了,但如果我们要得到字母和数字的随机四位数,那我们该如何写函数呢?下面胡鹏博客在PHP资料栏目下给出一个完整的实例。
PHP;">
PHP
function GetfourStr($len)
{
$chars_array = array(
"0","1","2","3","4","5","6","7","8","9","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",);
$charsLen = count($chars_array) - 1;
$outputstr = "";
for ($i=0; $i<$len; $i++)
{
$outputstr .= $chars_array[mt_rand(0,$charsLen)];
}
return $outputstr;
}
echo GetfourStr(4);
?>
其中部分函数解析:mt_rand函数说明:mt_rand()返回随机整数。 如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 RAND_MAX 之间的伪随机数。例如想要 0 到 46(包括 0 和 46)之间的随机数,用 mt_rand(0,46)。
原文链接:https://www.f2er.com/php/22887.html