Also send a function to generate random strings

WBOY
Release: 2016-07-25 08:50:28
Original
790 people have browsed it
  1. /**
  2. * Generate random numbers
  3. *
  4. * @param int $length Generate string length
  5. * @param int $type String type
  6. * @param bool $special Whether to use special characters
  7. * @return string Return the generated random string
  8. * @example random(10, null, true);
  9. */
  10. function random($length, $type = NULL, $special = FALSE) {
  11. $str = "";
  12. switch ($type) {
  13. case 1:
  14. $str = "0123456789";
  15. break;
  16. case 2:
  17. $str = "abcdefghijklmnopqrstuvwxyz";
  18. break;
  19. case 3:
  20. $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  21. break;
  22. case 4:
  23. $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  24. break;
  25. default:
  26. $str = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
  27. break;
  28. }
  29. return substr(str_shuffle(($special != FALSE) ? '!@#$%^&*()_+' . $str : $str), 0, $length);
  30. }
复制代码


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!