Home > Backend Development > PHP Tutorial > PHP random string generation function

PHP random string generation function

WBOY
Release: 2016-07-25 08:51:21
Original
1021 people have browsed it
  1. function random_string($length, $max=FALSE)
  2. {
  3. if (is_int($max) && $max > $length)
  4. {
  5. $length = mt_rand($length, $max);
  6. }
  7. $output = '';
  8. for ($i=0; $i<$length; $i++)
  9. {
  10. $which = mt_rand(0,2);
  11. if ($which === 0)
  12. {
  13. $output .= mt_rand(0,9);
  14. }
  15. elseif ($which === 1)
  16. {
  17. $output .= chr(mt_rand(65,90));
  18. }
  19. else
  20. {
  21. $output .= chr(mt_rand(97,122));
  22. }
  23. }
  24. return $output;
  25. }
复制代码


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template