时间:2021-01-14 11:14:22 | 栏目:PHP代码 | 点击:次
本文实例为大家分享了php生出随机字符串的具体代码,供大家参考,具体内容如下
function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } echo generateRandomString(20); /** * 随机生成字符串 * @param int $length * @return null|string */ private static function getRandChar($length = 8){ $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen($strPol)-1; for($i=0;$i<$length;$i++){ $str.=$strPol[rand(0,$max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数 } return $str; }