Generate random string with PHP

Submitted by:David Villa

Date added:18 June, 2013

Category:PHP

Define allowed characters in $s and length for the string in $l

Tags: random string

Code Snippet:

function rand_string($l){ 
$s= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
for($i=0; $i<$l; $i++) {
$rand.= $s[rand()%strlen($s)];
}
return $rand;
}
 
 

Comments