Home  >  Article  >  Backend Development  >  Also send a function to generate random strings

Also send a function to generate random strings

WBOY
WBOYOriginal
2016-07-25 08:50:28780browse
  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. }
复制代码


Statement:
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