Created
May 23, 2013 07:10
-
-
Save binjoo/5633221 to your computer and use it in GitHub Desktop.
PHP:生成随机字符串
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 生成随机字符串 | |
* | |
* @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