Posted By

niyi_gbads on 07/31/06


Tagged

random


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

luman
hudge
oli964
crs0328


Random Number Generator


 / Published in: PHP
 

This snippet generates a random string of random length

  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
Posted By: niyi_gbads on July 31, 2006

fanstatic addition

You need to login to post a comment.