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 App\Model\Tools; use Illuminate\Database\Eloquent\Model; 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 App\Model\Tools; use Illuminate\Database\Eloquent\Model; 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 />\n"; 4 echo uuid_create(1); //建議用法
刷新幾次頁面,觀察一下兩行UUID的變化,有什么發現嗎?想進一步了解的話,請學習一下UUID的幾個版本是如何定義的吧。
五、安裝擴展可能遇到的問題
安裝擴展遇到問題一般都是系統缺少相關組件造成的。
在centos 7中,需要先安裝libuuid-devel,這個用yum命令就可以了。
在mac os 10.9中則需要先安裝libuuid,這個要到libuuid | SourceForge.net下載。