熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

PHP隨機獲得字符代碼

2022-06-13   來源: PHP編程 
<?php
//獲取隨機字符 此函數區分字符大小寫 如果不區分大小寫可加入函數strtolower
function genRandomString($len)
{
    $chars = array(
        "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k"  
        "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"  
        "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G"  
        "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R"  
        "S" "T" "U" "V" "W" "X" "Y" "Z" "" "" ""  
        "" "" "" "" "" "" ""
    );
    $charsLen = count($chars) ;
    shuffle($chars);// 將數組打亂
    $output = "";
    for ($i=; $i<$len; $i++)
    {
        $output = $chars[mt_rand( $charsLen)]; //獲得一個數組元素
    }  
    return $output;
}
$str = genRandomString();
echo $str;
?>
  
 
From:http://tw.wingwit.com/Article/program/PHP/201311/21158.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.