最近在改 wordpress 的代码,需要用到 uuid。但是,php 中居然没有生成 uuid 的函数,只好自己写一个。
1
2
3
4
5
6
7
8
9
10
11
if (!function_exists('com_create_guid')) {
function com_create_guid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
上述代码可以生成一个 uuid version 4。uuid 目前有 5 个版本,其中第四版是完全随机的,生成起来比较容易。而其中的 com_create_guid,是 windows 中 php 的一个函数,它直接调用 com 的 createguid 函数来生成 uuid,但是在 linux 没有对应的函数库,只好自己写了。为了方便在不同的平台上使用,就创建了一个同名的函数。其它的代码就是生成随机数了。
至于用法,就直接调用 com_create_guid() 即可。
http://www.bkjia.com/phpjc/1014275.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1014275.htmltecharticlephp中生成uuid自定义函数分享 uuid 全称是 universally unique identifier,它是一种识别符,使用任意的计算机都可以生成,不需要一个中央数据库进...