Home >Backend Development >PHP Tutorial >Generate random strings that can be expanded by yourself

Generate random strings that can be expanded by yourself

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:47:12951browse
Generate a random string, which can be expanded by yourself
$type can be: upper (only generates uppercase letters), lower (only generates lowercase letters), number (only generates numbers)
$len is the length, defining the length of the string
  1. /***************************
  2. * Generate random strings and can be expanded by yourself
  3. * $type can be: upper (only uppercase letters are generated), lower (only lowercase letters are generated), number (only numbers are generated)
  4. * $len is the length, defining the string length
  5. ************************ ****/
  6. function _random($type,$len)
  7. {
  8. $new = '';
  9. $string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //Random number pool
  10. if($type= ='upper')
  11. {
  12. for($i = 0;$i<$len;$i++)
  13. {
  14. $new .= $string[mt_rand(36,61)];
  15. }
  16. return $new;
  17. }
  18. if($type == 'lower')
  19. {
  20. for($i = 0;$i<$len;$i++)
  21. {
  22. $new .= $string[mt_rand(10,35)];
  23. }
  24. return $new;
  25. }
  26. if($type == 'number')
  27. {
  28. for($i = 0;$i<$len;$i++)
  29. {
  30. $new .= $string[mt_rand( 0,9)];
  31. }
  32. return $new;
  33. }
  34. }
Copy code


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