Random Number Generator


/ Published in: PHP
Save to your folder(s)

This snippet generates a random string of random length


Copy this code and paste it in your HTML
  1. function str_rand ($minlength, $maxlength, $useupper=true, $usespecial=false, $usenumbers=true) {
  2.  
  3. $key ="";
  4. $charset = "abcdefghijklmnopqrstuvwxyz";
  5. if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  6. if ($usenumbers) $charset .= "0123456789";
  7. if ($usespecial) $charset .= "~@#$%^*()_+-={}|]["; // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./";
  8. if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength);
  9. else $length = mt_rand ($minlength, $maxlength);
  10. for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))];
  11. return $key;
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.