Return to Snippet

Revision: 691
at July 31, 2006 08:07 by niyi_gbads

Initial Code
function str_rand ($minlength, $maxlength, $useupper=true, $usespecial=false, $usenumbers=true) { 

$key ="";
    $charset = "abcdefghijklmnopqrstuvwxyz"; 
    if ($useupper)   $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    if ($usenumbers) $charset .= "0123456789"; 
    if ($usespecial) $charset .= "[email protected]#$%^*()_+-={}|][";   // Note: using all special characters this reads: "[email protected]#$%^&*()_+`-={}|\\]?[\":;'><,./"; 
    if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength); 
    else                         $length = mt_rand ($minlength, $maxlength); 
    for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 
    return $key; 

Initial URL

Initial Description
This snippet generates a random string of random length

Initial Title
Random Number Generator

Initial Tags

Initial Language