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



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);// "}"
  return $uuid;
 }
}
echo guid();
?>

以上所述是www.887551.com给大家介绍的php中生成标准uuid(guid)的方法详解整合,希望对大家有所帮助