Generate a class with any string length (customizable)
Customizable length, letters, numbers, uppercase and lowercase
-
- /*
- * The class that generates random strings. by default, only numbers and uppercase/lowercase letters are allowed.
- * @ Author Jerry
- */
- Class randomString {
- /*
- * Character settings of the generated string
- */
- Const NUMERIC_ONLY = 1; // only contains numbers
- Const LETTER_ONLY = 2; // only contains letters
- Const MIXED = 3; // MIXED numbers and letters
- /*
- * Input variables are string lengths, including letters, and whether uppercase letters are included.
- */
- Protected $ length, $ type, $ upper;
- /*
- * Parameter initialization
- * @ Param int, $ length string length
- * @ Param const, $ type indicates the type of the string generated.
- * @ Param boolean, $ whether upper contains uppercase letters
- */
- Public function _ construct ($ length = 16, $ type = self: MIXED, $ upper = true ){
- $ This-> length = $ length;
- $ This-> type = $ type;
- $ This-> upper = $ upper;
- }
- /*
- * Called when the object is converted to a string
- * @ Return string
- */
- Public function _ toString (){
- Return $ this-> pickUpChars ();
- }
- /*
- * Generate random strings
- * @ Global $ type
- * @ Return string, $ string
- */
- Public function pickUpChars (){
- Switch ($ this-> type ){
- Case self: NUMERIC_ONLY:
- $ Raw = '000000 ';
- Break;
- Case self: LETTER_ONLY:
- $ Raw = 'qwertyuioplkjhgfdsazxcvbnm '.
- 'Qwertyuioplkjhgfdsazxcvbnm ';
- Break;
- Default:
- $ Raw = 'qwertyuioplkjhgfdsazxcvbnm '.
- 'Qwertyuioplkjhgfdsazxcvbnm '.
- '123 ';
- Break;
- }
- $ String = '';
- For ($ index = 0; $ index <$ this-> length; $ index ++)
- $ String. = substr ($ raw, mt_rand (0, strlen ($ raw)-1), 1 );
- If (! $ This-> upper)
- $ String = strtolower ($ string );
- Return $ string;
- }
- }
- // Echo new randomString (170, randomString: MIXED, TRUE ).'
';
|