Posted By

tucq88 on 08/17/12


Tagged

php random String codeigniter


Versions (?)

random string


 / Published in: PHP
 

Copied from CodeIgniter

  1. function random_string($type = 'alnum', $len = 8) {
  2. switch($type) {
  3. case 'basic' :
  4. return mt_rand();
  5. break;
  6. case 'alnum' :
  7. case 'numeric' :
  8. case 'nozero' :
  9. case 'alpha' :
  10. switch ($type) {
  11. case 'alpha' :
  12. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  13. break;
  14. case 'alnum' :
  15. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  16. break;
  17. case 'numeric' :
  18. $pool = '0123456789';
  19. break;
  20. case 'nozero' :
  21. $pool = '123456789';
  22. break;
  23. }
  24.  
  25. $str = '';
  26. for ($i=0; $i < $len; $i++) {
  27. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  28. }
  29. return $str;
  30. break;
  31. case 'unique' :
  32. case 'md5' :
  33. return md5(uniqid(mt_rand()));
  34. break;
  35. break;
  36. }
  37. }

Report this snippet  

You need to login to post a comment.