经测试代码如下:
/**
* 生成一个随机的字符串
*
* @param int $length
* @param boolean $special_chars
* @return string
* @author 编程之家 jb51.cc jb51.cc
*/
function getRandStr($length = 12,$special_chars = true) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
if ($special_chars) {
$chars .= '!@#$%^&*()';
}
$randStr = '';
for ($i = 0; $i < $length; $i++) {
$randStr .= substr($chars,mt_rand(0,strlen($chars) - 1),1);
}
return $randStr;
}