Posted By

dimkalinux on 05/30/13


Tagged


Versions (?)

Random 2


 / Published in: PHP
 

  1. /**
  2. * Generate random string.
  3. * @param int
  4. * @param string
  5. * @return string
  6. */
  7. public static function random($length = 10, $charlist = '0-9a-z')
  8. {
  9. $charlist = str_shuffle(preg_replace_callback('#.-.#', function($m) {
  10. return implode('', range($m[0][0], $m[0][2]));
  11. }, $charlist));
  12. $chLen = strlen($charlist);
  13.  
  14. static $rand3;
  15. if (!$rand3) {
  16. $rand3 = md5(serialize($_SERVER), TRUE);
  17. }
  18.  
  19. $s = '';
  20. for ($i = 0; $i < $length; $i++) {
  21. if ($i % 5 === 0) {
  22. list($rand, $rand2) = explode(' ', microtime());
  23. $rand += lcg_value();
  24. }
  25. $rand *= $chLen;
  26. $s .= $charlist[($rand + $rand2 + ord($rand3[$i % strlen($rand3)])) % $chLen];
  27. $rand -= (int) $rand;
  28. }
  29. return $s;
  30. }
  31.  

Report this snippet  

You need to login to post a comment.