PHP生成不重复随机字符串(数字+英文)

发表于 : 2017-11-10   · CC BY-NC-SA 4.0 ·   1677 人浏览

使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,保证不重复

function  Random ($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;
}

echo Random(2); // 建议使用 2 代表12位字符串

 

PHP 随机字符串 随机密码