<? Phpfunction rand_str ($ length, $ P = 'all') {$ Nums = '000000'; $ lowers = 'abcdefghijklmnopqrstuvwxy'; $ uppers = 'abcdefghijklmnopqrstuvwxy '; if ($ P = 'all') {$ src = $ nums. $ lowers. $ uppers;} else {$ src = ''; If (strpos ($ P, 'num ')! = False) $ SRC. = $ Nums; If (strpos ($ P, 'lower ')! = False) $ SRC. = $ lowers; If (strpos ($ P, 'uppper ')! = False) $ SRC. = $ uppers;} return $ SRC? Substr (str_shuffle ($ SRC), 0, $ length): $ SRC ;}?>
I searched the PHP functions of random strings on the Internet and found that most implementations use loops. This is a little inefficient. in PHP, there are all kinds of functions that you can't think. the str_shuffle () function can easily implement random strings. however, we recommend you encapsulate it. After all, some people only need to use uppercase letters and only numbers.
The second parameter num, lower, and upper of the function can be combined at will.
The true core statement is only one line
Substr (str_shuffle ($ SRC), 0, $ length)
The function is to unsequence the string $ SRC and intercept the first $ length characters.