Generate a Random String

Submitted by:Jhon Brain

Date added:18 September, 2013

Category:PHP

If you don’t need human readable string, then use this function instead, which will create a random string you can use for user id’s etc.

Tags: random string , generate string

Code Snippet:

*@l - length of random string 
*/
function generate_rand($l){
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
for($i=0; $i<$l; $i++) {
$rand.= $c[rand()%strlen($c)];
}
return $rand;
}
 
 

Comments