Sharing of custom functions to generate UUID in PHP
The full name of UUID is Universally unique identifier. It is an identifier that can be generated by any computer and does not require a central database. Management ensures that there is almost no chance of duplication. The value range of UUID is so large that it is said that every grain of sand in the world is assigned a UUID, and there will be no duplicates.
Recently changing the WordPress code requires the use of UUID. However, there is no function to generate UUID in PHP, so I had to write one myself.
1
2
3
4
5
6
7
8
9
10
11
|
if (!function_exists('com_create_guid')) {
function com_create_guid() {
return sprintf( 'xx-x-x-x-xxx',
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 )
);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
if (!function_exists('com_create_guid')) {
function com_create_guid() {
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 )
);
}
}
|
The above code can generate a UUID version 4. There are currently 5 versions of UUID, of which the fourth version is completely random and easier to generate. Among them, com_create_guid is a function of PHP in Windows. It directly calls COM's CreateGuid function to generate UUID. However, there is no corresponding function library in Linux, so I have to write it myself. In order to facilitate use on different platforms, a function with the same name was created. The other code is to generate random numbers.
As for usage, just call com_create_guid() directly.
http://www.bkjia.com/PHPjc/1014275.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1014275.htmlTechArticleGenerate UUID custom function in PHP to share UUID. The full name is Universally unique identifier. It is an identifier that can be used in any way. It can be generated by any computer and does not require a central database...