Home  >  Article  >  Backend Development  >  How to generate random strings in php without repeating them

How to generate random strings in php without repeating them

藏色散人
藏色散人Original
2020-08-12 10:26:144051browse

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.

How to generate random strings in php without repeating them

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.&#39;<br>&#39;;
}
for($j=0;$j<1200;$j++){
$a= random_str(18);
//$a=substr(md5(date(&#39;YmdHis&#39;).rand(1000,9999)),   8,   16);
//echo $a.&#39;<br><br>&#39;;
}

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn