1. 程式人生 > >PHP生成隨機或者唯一字符串

PHP生成隨機或者唯一字符串

script else scrip amp 生成 int array bre abcde

本文出至:新太潮流網絡博客

/**
 * [生成隨機字符串]
 * @E-mial [email protected]
 * @TIME   2017-04-07
 * @WEB    http://blog.iinu.com.cn
 * @param  integer $length [生成的長度]
 * @param  integer $type   [生成的類型]
 * @return [type]   str       [description]
 * @php 隨機碼類型:0,數字+大寫字母;1,數字;2,小寫字母;3,大寫字母;4,特殊字符;-1,數字+大小寫字母+特殊字符
 */
function randCode($length = 5, $type = 0) {
    $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "[email protected]#$%^&*(){}[]|");
    if ($type == 0) {
        array_pop($arr);
        $string = implode("", $arr);
    } else if ($type == "-1") {
        $string = implode("", $arr);
    } else {
        $string = $arr[$type];
    }
    $count = strlen($string) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str[$i] = $string[rand(0, $count)];
        $code .= $str[$i];
    }
    return $code;
}

/**
 * [生成唯一字符串]
 * @E-mial [email protected]
 * @TIME   2017-04-07
 * @WEB    http://blog.iinu.com.cn
 * @ 0-存數字字符串;1-小寫字母字符串;2-大寫字母字符串;3-大小寫數字字符串;4-字符;
 *   5-數字,小寫,大寫,字符混合
 * @param  integer $type   [字符串的類型]
 * @param  integer $length [字符串的長度]
 * @param  integer $time   [是否帶時間1-帶,0-不帶]
 * @return [string]  $str    [返回唯一字符串]
 */
function randSole($type = 0,$length = 18,$time=0){
    $str = $time == 0 ? ‘‘:date(‘YmdHis‘,time());
    switch ($type) {
        case 0:
            for((int)$i = 0;$i <= $length;$i++){
                if(mb_strlen($str) == $length){
                    $str = $str;
                }else{
                    $str .= rand(0,9);
                }
            }
            break;
        case 1:
            for((int)$i = 0;$i <= $length;$i++){
                if(mb_strlen($str) == $length){
                    $str = $str;
                }else{
                    $rand = "qwertyuioplkjhgfdsazxcvbnm";
                    $str .= $rand{mt_rand(0,26)};
                }
            }
            break;
        case 2:
            for((int)$i = 0;$i <= $length;$i++){
                if(mb_strlen($str) == $length){
                    $str = $str;
                }else{
                    $rand = "QWERTYUIOPLKJHGFDSAZXCVBNM";
                    $str .= $rand{mt_rand(0,26)};
                }
            }
            break;
        case 3:
            for((int)$i = 0;$i <= $length;$i++){
                if(mb_strlen($str) == $length){
                    $str = $str;
                }else{
                    $rand = "123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
                    $str .= $rand{mt_rand(0,35)};
                }
            }
            break;
        case 4:
            for((int)$i = 0;$i <= $length;$i++){
                if(mb_strlen($str) == $length){
                    $str = $str;
                }else{
                    $rand = "[email protected]#$%^&*()_+=-~`";
                    $str .= $rand{mt_rand(0,17)};
                }
            }
            break
;case5:for((int)$i =0;$i <= $length;$i++){if(mb_strlen($str)== $length){ $str = $str;}else{ $rand ="[email protected]#$%^&*()_+=-~`"; $str .= $rand{mt_rand(0,52)};}}break;}return $str;}

本文出至:新太潮流網絡博客

PHP生成隨機或者唯一字符串