PHP随机获得一个字符串
程序员文章站
2022-07-14 19:54:01
...
/**
* 获取随机字符串
* @param int $len 字符串长度
* @param string $chars 字符源
* @return string
*/
function getRandomString($len, $chars = null)
{
if (is_null($chars))
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
$str .= $chars[mt_rand(0, $lc)];
}
return $str;
}