2008-04-11, 08:22 AM
Hi i would suggest a more robust random_str function,
i saw your implementation and sincerely i don't like the fact that you insert the character into an array.
I wrote one that i think it is better as it takes char directly from ASCII table instead of an array.
Also you should use mt_rand instead of rand, as it is faster (i tested )
i saw your implementation and sincerely i don't like the fact that you insert the character into an array.
I wrote one that i think it is better as it takes char directly from ASCII table instead of an array.
function random_str($len = 8)
{
$retn;
for($i = 0; $i < $len; $i++)
{
$type = mt_rand(1,3);
//1: Upper Char (A-Z); 2: Lower Char(a-z); 3: numeric char(0-9)
switch($type)
{
case 1:
$retn .= chr(mt_rand(0x41, 0x5A));
break;
case 2:
$retn .= chr(mt_rand(0x61, 0x7A));
break;
case 3:
$retn .= chr(mt_rand(0x30, 0x39));
break;
}
}
return $retn;
}
Also you should use mt_rand instead of rand, as it is faster (i tested )