PHP生成不重復(fù)字符串
使用時(shí)間戳作為原始字符串,再隨機(jī)生成五個(gè)字符隨機(jī)插入任意位置,生成新的字符串,保證不重復(fù)。
代碼如下:
function rand($len) { $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $string=time(); for(;$len>=1;$len--) { $position=rand()%strlen($chars); $position2=rand()%strlen($string); $string=substr_replace($string,substr($chars,$position,1),$position2,0); } return $string; }
推薦教程:PHP視頻教程