Because the tool requires us to write a function that can generate random strings. I think there should be a lot of such random string generation functions on the internet. Baidu has a bunch of functions. below I will summarize them for you.
Because the tool requires us to write a function that can generate random strings. I think there should be a lot of such random string generation functions on the Internet, baidu has a bunch of good php functions below.
Use the for loop to traverse the defined characters. the code is as follows:
-
- /* Generate Password
- * Length: 8
- */
- $ Str = "0123456789abcdefghijklmnopqrstuvwxyz"; // output character set
- $ N = 8; // output string length
- $ Len = strlen ($ str)-1;
- For ($ I = 0; $ I <$ n; $ I ++ ){
- $ S. = $ str [rand (0, $ len)];
- }
- Echo $ s."
";
- ?>
You can generate a string of numbers, character strings, and so on (uppercase, lowercase, uppercase, and lowercase, and a combination of uppercase and lowercase numbers can also be expanded as needed ).
The following $ length = 5 is 10 digits if you change it to 10.
Change $ str = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz 'to $ str = '000000'. the code is as follows:
-
- Function getRandStr ($ length ){
- $ Str = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
- $ RandString = '';
- $ Len = strlen ($ str)-1;
- For ($ I = 0; $ I <$ length; $ I ++ ){
- $ Num = mt_rand (0, $ len );
- $ RandString. = $ str [$ num];
- }
- Return $ randString;
- }
- // Use the following method:
- $ Test = getRandStr ($ length = 5 );
- Echo $ test;
- ?>
Or use while, the code is as follows:
-
- /**
- */
- Function createRandomStr ($ length ){
- $ Str = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '; // 62 characters
- $ Strlen = 62;
- While ($ length> $ strlen ){
- $ Str. = $ str;
- $ Strlen + = 62;
- }
- $ Str = str_shuffle ($ str );
- Return substr ($ str, 0, $ length );
- }
- Echo createRandomStr (10 );
- ?>
The code of array and character conversion is as follows:
-
- /**
- * @ Blog
- */
- Function createRandomStr ($ length ){
- $ Str = array_merge (range (0, 9), range ('A', 'z'), range ('A', 'z '));
- Shuffle ($ str );
- $ Str = implode ('', array_slice ($ str, 0, $ length ));
- Return $ str;
- }
- Echo createRandomStr (10 );
- ?>