Objective
In general, when we are doing the program, there will certainly be many places to use the random string, such as to do the verification code used, and then the function is encapsulated, use time to set 2 parameters, the principle is to randomly fetch the string, concatenation of the string
$str set the string to be collected, such as
$str =´jfowef34098094j3204efa234sfg2z23srhftj345xjxjhsrth´;
Then the string generated inside the function is randomly fetched from the $STR
$codeLen set the random string to be generated, set 5, then generate 5 random strings, such as
$codeLen =´5´;//Sets the number of random numbers generated
The code is as follows
<?php//mt_rand Gets the random number Mt_rand (min, max);//sets the randomly collected string $str= " ABCDEFGHIJKMNPQRSTUVWXYZ0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ "; Sets the number of random numbers generated $codelen=´5´; function Str_rand ($STR, $codeLen) {$rand = ""; for ($i =0; $i < $codeLen-1; $i) { //AS: Random number 30: $str [+] $rand. = $ Str[mt_rand (0, strlen ($str)-1)]; } return $rand;} $code =str_rand ($str, $codeLen); Echo $code;?>
Summarize
The above is the complete method of generating random strings, in addition to the verification code can be used anywhere else, I hope that the content of this article on everyone's study or work can bring some help, if there are questions you can message exchange.