TIP

There are many ways of generating random strings.

Some of them i've written below....

I'v tested the processing time for each script independtly no significant time difference was found.

1. Fetching random character from unicode values...
<?php
//way 1 

$str="";

for($i=0;$i<10;$i++)
{
$ran=rand(0,2);
switch($ran)
{
case 0 : $ran_char=chr(mt_rand(48,57)); 

break;
case 1 : $ran_char=chr(mt_rand(65,90)); 

break;
case 2 : $ran_char=chr(mt_rand(97,122)); 

break;
default : break;
}
$str=$str.$ran_char;

}

echo $str."<br>";
2. Appending random character from a string($scram) provided...
//way 2

$scram="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; ////prob 26/61(.4262)+26/61(.4262)+9/61(.1475)
$s="";
for($i=0;$i<10;$i++)
{
$s=$s.substr($scram,mt_rand(0,61),1);

}
echo $s."<br>";
3. By str_shuffle() function ...
  
//way 3
echo substr(str_shuffle( $scram ), 0, 10 ); 

?>


Leave a Comment  
Download Code(36)





All rights reserved, the content is copyrighted to the author.

RELATED POST