PHP自动生成随绝密码

WBOY
Release: 2016-06-13 12:56:34
Original
790 people have browsed it

PHP自动生成随机密码

<?php

function rand_string($len = 16, $keyword = '') {
    if (strlen($keyword) > $len) {//关键字不能比总长度长
        return false;
    }
    $str = '';
    $chars = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHIJKMNPQRSTUVWXYZ'; //去掉1跟字母l防混淆            
    if ($len > strlen($chars)) {//位数过长重复字符串一定次数
        $chars = str_repeat($chars, ceil($len / strlen($chars)));
    }
    $chars = str_shuffle($chars); //打乱字符串
    $str = substr($chars, 0, $len);
    if (!empty($keyword)) {
        $start = $len - strlen($keyword);
        $str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //从随机位置插入关键字
    }
    return $str;
}

echo rand_string(16,"ab"); //output example:V8bNY6SmkeywordB
?>
Copy after login

?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!