首頁  >  文章  >  後端開發  >  PHP生成随机数,字符串_PHP教程

PHP生成随机数,字符串_PHP教程

WBOY
WBOY原創
2016-07-13 17:51:45935瀏覽

function rand_string($len = 6, $type = '', $addChars = '') { 
    $str = ''; 
    switch ($type) { 
        case 0 : 
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars; 
            break; 
        case 1 : 
            $chars = str_repeat ( '0123456789', 3 ); 
            break; 
        case 2 : 
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars; 
            break; 
        case 3 : 
            $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars; 
            break; 
        default : 
            // 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数 
            $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars; 
            break; 
    } 
    if ($len > 10) { //位数过长重复字符串一定次数 
        $chars = $type == 1 ? str_repeat ( $chars, $len ) : str_repeat ( $chars, 5 ); 
    } 
    if ($type != 4) { 
        $chars = str_shuffle ( $chars ); 
        $str = substr ( $chars, 0, $len ); 
    } else { 
        // 中文随机字 
        for($i = 0; $i             $str .= msubstr ( $chars, floor ( mt_rand ( 0, mb_strlen ( $chars, 'utf-8' ) - 1 ) ), 1 ); 
        } 
    } 
    return $str; 

 

 

摘自 lpdx111的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478170.htmlTechArticlefunction rand_string($len = 6, $type = , $addChars = ) { $str = ; switch ($type) { case 0 : $chars = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz . $addChars; break; case 1...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn