1. 生成唯一的ID
uniqid();//不加前缀,生成的字串是13位,根据当前微妙的时间戳
2. 随机函数
/**
* 生成指定位数的随机字符串
*
* @param int $len 随机字符串长度
* @param bool $include_number 是否允许包含数字
*
*/
public static function make_rand_string($len, $include_number = false) {
$string_list = array (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘ );
$string_include_number_list = array (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘M‘, ‘N‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ );
if ($include_number) {
$arr = $string_include_number_list;
} else {
$arr = $string_list;
}
$rand_max = count ( $arr ) - 1;
$return_string = null;
for($i = 0; $i < $len; $i ++) {
$rand = mt_rand ( 0, $rand_max );
$return_string .= $arr [$rand];
}
return $return_string;
}
3. 写的再乱 也要坚持写博客
PHP-生成唯一ID串