生成随机字符唯一标识符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支持多种数据类型,包括数值、字符串、日期和时间等类型,以...
2024-11-20 20:23:37
MySQL变量类型列表
MySQL变量类型包括数值、字符串、日期和时间等类型,用于存储不同...
2024-11-20 19:46:45
MySQL变量数据类型介绍
MySQL支持多种数据类型,包括数值、字符串、日期时间及枚举和集合...
2024-11-20 19:23:39