php常用方法之随机字符串

function random_str($len=10, $type=1){
    switch($type){
        case 2:
            $chars='0123456789';
            break;
        case 3:
            $chars='abcdefghijklmnopqrstuvwxyz';
            break;
        case 4:
            $chars='ABDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 5:
            $chars='abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 6:
            $chars='abcdefghijklmnopqrstuvwxyz0123456789';
            break;
        default:
            $chars='abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            break;
    }
    $str = '';
    for ( $i = 0; $i <$len; $i++ ){
        $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $str;
}

未经允许不得转载:任鹏个人博客 » php常用方法之随机字符串

赞 (0) 打赏

评论 0

取消
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏