Generate a class with any string length (customizable)

Source: Internet
Author: User
Generate a class with any string length (customizable)
Customizable length, letters, numbers, uppercase and lowercase

  1. /*
  2. * The class that generates random strings. by default, only numbers and uppercase/lowercase letters are allowed.
  3. * @ Author Jerry
  4. */
  5. Class randomString {
  6. /*
  7. * Character settings of the generated string
  8. */
  9. Const NUMERIC_ONLY = 1; // only contains numbers
  10. Const LETTER_ONLY = 2; // only contains letters
  11. Const MIXED = 3; // MIXED numbers and letters
  12. /*
  13. * Input variables are string lengths, including letters, and whether uppercase letters are included.
  14. */
  15. Protected $ length, $ type, $ upper;
  16. /*
  17. * Parameter initialization
  18. * @ Param int, $ length string length
  19. * @ Param const, $ type indicates the type of the string generated.
  20. * @ Param boolean, $ whether upper contains uppercase letters
  21. */
  22. Public function _ construct ($ length = 16, $ type = self: MIXED, $ upper = true ){
  23. $ This-> length = $ length;
  24. $ This-> type = $ type;
  25. $ This-> upper = $ upper;
  26. }
  27. /*
  28. * Called when the object is converted to a string
  29. * @ Return string
  30. */
  31. Public function _ toString (){
  32. Return $ this-> pickUpChars ();
  33. }
  34. /*
  35. * Generate random strings
  36. * @ Global $ type
  37. * @ Return string, $ string
  38. */
  39. Public function pickUpChars (){
  40. Switch ($ this-> type ){
  41. Case self: NUMERIC_ONLY:
  42. $ Raw = '000000 ';
  43. Break;
  44. Case self: LETTER_ONLY:
  45. $ Raw = 'qwertyuioplkjhgfdsazxcvbnm '.
  46. 'Qwertyuioplkjhgfdsazxcvbnm ';
  47. Break;
  48. Default:
  49. $ Raw = 'qwertyuioplkjhgfdsazxcvbnm '.
  50. 'Qwertyuioplkjhgfdsazxcvbnm '.
  51. '123 ';
  52. Break;
  53. }
  54. $ String = '';
  55. For ($ index = 0; $ index <$ this-> length; $ index ++)
  56. $ String. = substr ($ raw, mt_rand (0, strlen ($ raw)-1), 1 );
  57. If (! $ This-> upper)
  58. $ String = strtolower ($ string );
  59. Return $ string;
  60. }
  61. }
  62. // Echo new randomString (170, randomString: MIXED, TRUE ).'
    ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.