Home >Backend Development >PHP Tutorial > PHP自动生成随绝密码

PHP自动生成随绝密码

WBOY
WBOYOriginal
2016-06-13 12:56:34875browse

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
?>

?

Statement:
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