Home >Backend Development >PHP Problem >How to generate random strings in php without repeating them
How to implement php to generate random strings without repetition: first create a PHP sample file; then define a "random_str" method; then use functions such as "array_merge" to generate a string containing uppercase English letters, lowercase English letters An array of letters and numbers will do.
Recommended: "PHP Video Tutorial"
php generates random strings (no duplication)
function random_str($length) { //生成一个包含 大写英文字母, 小写英文字母, 数字 的数组 $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z')); $str = ''; $arr_len = count($arr); for ($i = 0; $i < $length; $i++) { $rand = mt_rand(0, $arr_len-1); $str.=$arr[$rand]; } return $str.'<br>'; } for($j=0;$j<1200;$j++){ $a= random_str(18); //$a=substr(md5(date('YmdHis').rand(1000,9999)), 8, 16); //echo $a.'<br><br>'; }
The above is the detailed content of How to generate random strings in php without repeating them. For more information, please follow other related articles on the PHP Chinese website!