在编写PHP实用程序时,经常用到产生定长随机字符串以用于临时用户ID号或用户密码等。现我将其写成一个函数与大家共享。
<?
function MakeRandom($length)
{
$possible = "0123456789!@#$%^&*()_+".
"abcdefghijklmnopqrstuvwxyz".
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str = "";
while(strlen($str) < $length)
{
$str .= substr($possible, (rand() % strlen($possible)), 1);
}
return($str);
}
?>
大家可以根据实际应用,对上述函数的字符取值范围进行修改。
|