DeTechn Blog

php获取随机字符串

/*
 * 获取随机字符串
 * type: 0:随机字符串,1:数字,2:a-zA-Z,3:包含特殊字符的字符串
 * len: 字符串长度
 */
function RandStr($length = 6, $type = 0)
{
    $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
    $code = '';
    if ($type == 0) {
        array_pop($arr);
        $string = implode("", $arr);
    } else if ($type == "-1") {
        $string = implode("", $arr);
    } else {
        $string = $arr[$type];
    }
    $count = strlen($string) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str[$i] = $string[rand(0, $count)];
        $code .= $str[$i];
    }
    return $code;
}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »