First: operate using string functions
Copy codeThe Code is as follows: <? Php
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 );
Method 2: Use the array and character conversion ideas:
Copy codeThe Code is as follows: <? Php
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 );
After 1000 tests in a loop, the first calculation method is more efficient (the first calculation method is about one thousand times, and the second calculation method is about 0.02 seconds )!