原理:
定義一個字符串,里面存放你想要的字符,這里是大小寫字母以及數字,
利用php自帶的rand()方法生成隨機數,根據結果字符串的長度,來調用幾次rand()函數,
每次隨機生成的數字就是字符串的下標,最后將所有字符組成新的字符串返回

1 <?php 2 3 /** 4 ** $length : the length of the result String 5 **/ 6 function getRandChar($length){ 7 $str = null; 8 $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";//大小寫字母以及數字 9 $max = strlen($strPol)-1; 10 11 for($i=0;$i<$length;$i++){ 12 $str.=$strPol[rand(0,$max)]; 13 } 14 return $str; 15 } 16 17 echo getRandChar(10); 18 ?>