Used to make verification codes, and then encapsulate this function. When using it, you need to set 2 parameters:
The string to be collected in the $str setting, such as:
$str='efasfgzsrhftjxjxjhsrth';
Then the string generated in the function will be randomly grabbed from efasfgzsrhftjxjxjhsrth;
$codeLen sets the random string to be generated. If set to 5, 5 random strings will be generated.
Principle: randomly grab strings and splice the strings
Effect:
Code:
<span style="font-size: 18px;"><?php //mt_rand 获取随机数 mt_rand(min, max); $str="abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ";//设置被随机采集的字符串 $codeLen='5';//设置生成的随机数个数 function str_rand($str,$codeLen){ $rand=""; for($i=0; $i<$codeLen-1; $i++){ $rand .= $str[mt_rand(0, strlen($str)-1)]; //如:随机数为30 则:$str[30] } return $rand; } $code=str_rand($str,$codeLen); echo $code;</span><br /><span style="font-size: 18px;">?></span>