生成随机字符串的函数

/**
 * 取出随机字符串函数
 * @param $length 取出字符串长度
 * @param $str  指定字符串集合
 * @param $lower 是否转为大写(true 大写  false 小写) 默认小写
 */
function randstr($length,$str='',$lower=false){
    if(empty($str)){
        $str = "abcdefghijklmnopqrstuvwxyz0123456789";
    }
    $strlength = strlen($str);
    $result = '';
    for ( $i = 0; $i < $length; $i++ )  {
        $result .= substr($str, mt_rand(0, strlen($str)-1), 1);
    }
    if($lower){
        $result = strtoupper($result);
    }
    return $result;
}

 

posted @ 2017-10-02 17:17  枫夜雪  阅读(531)  评论(0编辑  收藏  举报