/** * 产生随机字符串 * * @param int $length 输出长度 * @param string $chars 可选的 ,默认为 0123456789 * @return string 字符串 */ function random($length, $chars = '0123456789') { $hash = ''; $max = strlen($chars) - 1; mt_srand(); for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } $a=random(8, '1294567890abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ'); var_dump($a);///mnt/hgfs/www/test/index.php:28:string 'otFTNx9u' (length=8)