Random AlphaNumeric String


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

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.