Posted By

fabienthomas on 09/05/11


Tagged

random String


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

resting


Random AlphaNumeric String


 / Published in: PHP
 

just a random string generator function, usefull in captcha building process for example, or in case of random password needed.

  1. function randStr($length=6)
  2. {
  3. $str = "";
  4. $characters = "abcdefghjkmnpqrstwxyz123456789";
  5. $maxlength = strlen($characters);
  6. if ($length > $maxlength) {
  7. $length = $maxlength;
  8. }
  9. $i = 0;
  10. while ($i < $length) {
  11. $char = substr($characters, mt_rand(0, $maxlength-1), 1);
  12. if (!strstr($str, $char)) {
  13. $str .= $char;
  14. $i++;
  15. }
  16.  
  17. }
  18. return $str;
  19. }

Report this snippet  

You need to login to post a comment.