生成随机字符唯一标识符guid或uuid
未知
2024-05-27 13:48:44
0次
/**
* @param $length
* @return string
* 生成随机字符串
*/
function getNonceStr($length = 4)
{
$chars = "abcdefghijkmnpqrstuvwxyz23456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
// 生成10位邀请码,用户MD5的前4位和微秒的前6位,把4替换成6寓意好。
function createIntroducerCode($user){
$d=str_replace('4', '6', substr(md5($user), 0,4).substr(microtime(), 2, 6));
return $d;
}
//生成唯一订单号
function build_order_no(){
return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
//guid
if (!function_exists('guid')) {
function guid(){
$uuid = '';
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
//$uuid = //chr(123)// "{"
$uuid .= substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12);
//.chr(125);// "}"
return $uuid;
}
}
function createUUID(){
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
相关内容
MySQL枚举类型
MySQL枚举类型用于限制字段取值范围,提供数据一致性和存储效率。...
2025-02-08 15:46:40
MySQL大数据类型支持
MySQL支持多种大数据类型,包括整数、浮点数、字符、日期、二进制...
2025-02-08 15:00:40
CGI脚本开发流程详解
本文详细解析了CGI脚本的开发流程,包括需求确定、环境安装、编写代...
2025-01-28 20:46:43