Maison  >  Article  >  développement back-end  >  php生成uuid格式字符串实例程序

php生成uuid格式字符串实例程序

高洛峰
高洛峰original
2016-11-29 15:16:481155parcourir

uuid是什么格式的字符串我想很多朋友不知道,但是你己经来了估计就清楚什么是uuid了,下面我们一起来看看如何生成uuid字符串吧.

UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的,通常平台会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字.

由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长,关于UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers).

在ColdFusion中可以用CreateUUID()函数很简单的生成UUID,其格式为:xxxxxxxx-xxxx-xxxx- xxxxxxxxxxxxxxxx(8-4-4-16),其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字,而标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)

php生成uuid格式字符串实例程序实例代码如下:

function guid(){

if (function_exists('com_create_guid')){

return com_create_guid();

}else{

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)

// "{"

.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);

// "}" //开源代码phpfensi.com

return $uuid;

}

}

?> 


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn