PHP learning notes: universal random string generation function (encapsulated), php learning notes
The verification code is used, and then the function is encapsulated. You need to set two parameters when using the function:
$ Str: the string to be collected, for example:
$ Str = 'efasfgzsrhftjxjxjhsrth ';
Then the string generated in the function will be randomly crawled from efasfgzsrhftjxjhsrth;
$ CodeLen: Specifies the random string to be generated. If it is set to 5, five random strings are generated.
Principle: Randomly capture and splice strings
Effect:
Code:
<? Php // mt_rand obtain the random number mt_rand (min, max); $ str = "random"; // set the string to be randomly collected $ codeLen = '5 '; // set the number of random numbers generated function str_rand ($ str, $ codeLen) {$ rand = ""; for ($ I = 0; $ I <$ codeLen-1; $ I ++) {$ rand. = $ str [mt_rand (0, strlen ($ str)-1)]; // For example, if the random number is 30, return $ rand ;} $ code = str_rand ($ str, $ codeLen); echo $ code;
?>