奋斗我的青春

php生成随机字符串函数

发布时间:2个月前热度: 39 ℃评论数:

代码:

/**
 * 获取随机字符串
 * @param int		$minlength	最小长度
 * @param int		$maxlength	最大长度
 * @param boolean	$uselower	使用小写
 * @param boolean	$useupper	使用大写
 * @param boolean	$usespecial	使用字符
 * return string
 */
function PT_GenerateKey($minlength = 20, $maxlength = 20, $uselower = true, $useupper = true, $usenumbers = true, $usespecial = false) {
    $charset = '';
    if ($uselower) {
        $charset .= "abcdefghijklmnopqrstuvwxyz";
    }
    if ($useupper) {
        $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    }
    if ($usenumbers) {
        $charset .= "123456789";
    }
    if ($usespecial) {
        $charset .= "~@#$%^*()_+-={}|][";
    }
    if ($minlength > $maxlength) {
        $length = mt_rand($maxlength, $minlength);
    } else {
        $length = mt_rand($minlength, $maxlength);
    }
    $key = '';
    for ($i = 0; $i < $length; $i++) {
        $key .= $charset[(mt_rand(0, strlen($charset) - 1))];
    }
    return $key;
}

php,生成,随机,字符串,函数

栏目导航

  1. PHP
  2. WEB前端

手机扫码访问