php如何生成 uuid(总结)

php如何生成 uuid(总结)

一、总结

一句话总结:

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

1、UUID的格式是什么?

UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12),其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字

2、UUID使用实例?

用md5函数生成密码字符串,然后substr函数在里面截取就好:md5(uniqid(mt_rand(), true));
<?php

namespace AppModelTools;

use IlluminateDatabaseEloquentModel;

class UUID extends Model
{
    public static function create_uuid($prefix=""){
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );
        return $prefix.$uuid ;
    }
}

3、php的UUID扩展?

UUID extension

二、php生成唯一识别码uuid

转自或参考:php生成唯一识别码uuid
https://www.cnblogs.com/tine/p/9316509.html

/*生成唯一标志
*标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)
*/

function
uuid() { $chars = md5(uniqid(mt_rand(), true)); $uuid = substr ( $chars, 0, 8 ) . '-' . substr ( $chars, 8, 4 ) . '-' . substr ( $chars, 12, 4 ) . '-' . substr ( $chars, 16, 4 ) . '-' . substr ( $chars, 20, 12 ); return $uuid ; } echo uuid(); //Returns like 'dba5ce3e-430f-cf1f-8443-9b337cb5f7db'

三、laravel中uuid使用实例

<?php

namespace AppModelTools;

use IlluminateDatabaseEloquentModel;

class UUID extends Model
{
    public static function create_uuid($prefix=""){
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );
        return $prefix.$uuid ;
    }
}

四、PHP中生成UUID

转自或参考:PHP中生成UUID
https://www.cnblogs.com/cndavidwang/p/4018207.html

一、什么是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)

  简单的说UUID就是一串全球唯一的(16进制)数字串。

  UUID的全拼为“Universally Unique Identifier”,可以译为“通用唯一识别码”。UUID由开源软件基金会 (Open Software Foundation, OSF) 定义,是分布式计算环境 (Distributed Computing Environment, DCE) 的一个组成部分。

  UUID的标准格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx”,五个部分分别为8个字符、4个字符、4个字符、4个字符、12个字符,中间用“-”号间隔。常见的GUID(Globally Unique Identifier)是微软对UUID标准的一种实现。

二、为什么要使用UUID

  好处那叫一个多呀~~~,您随便百度把。

三、UUID的生成代码

  可以用扩展。

四、安装UUID扩展

  相关的扩展在这里:PECL :: Package :: uuid

  PHP扩展安装步骤一直就是那几个:

  

wget http://pecl.php.net/get/uuid-1.0.3.tgz
tar zxvf uuid-1.0.3.tgz
cd uuid-1.0.3
phpize
./configure
make
make install

  好了,然后编辑一下PHP配置文件,重启一下服务器,到phpinfo()去看效果吧:

  安装成功之后,写两行代码测试一下吧:

  

1 <?php
2 //uuid.php
3 echo uuid_create(), "<br />
";
4 echo uuid_create(1);    //建议用法

  刷新几次页面,观察一下两行UUID的变化,有什么发现吗?想进一步了解的话,请学习一下UUID的几个版本是如何定义的吧。

五、安装扩展可能遇到的问题

  安装扩展遇到问题一般都是系统缺少相关组件造成的。

  在centos 7中,需要先安装libuuid-devel,这个用yum命令就可以了。

  在mac os 10.9中则需要先安装libuuid,这个要到libuuid | SourceForge.net下载。

原文地址:https://www.cnblogs.com/Renyi-Fan/p/11870429.html