Skip to content

Instantly share code, notes, and snippets.

@binjoo
Created May 23, 2013 07:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binjoo/5633221 to your computer and use it in GitHub Desktop.
Save binjoo/5633221 to your computer and use it in GitHub Desktop.
PHP:生成随机字符串
/**
* 生成随机字符串
*
* @access public
* @param integer $length 字符串长度
* @param string $specialChars 是否有特殊字符
* @return string
*/
public static function randString($length, $specialChars = false) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
if ($specialChars) {
$chars .= '!@#$%^&*()';
}
$result = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$result .= $chars[rand(0, $max)];
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment