Created
February 15, 2020 19:26
-
-
Save dchest/e77e705ef2c120c262b58a7a4893df61 to your computer and use it in GitHub Desktop.
UUIDv4 generator for PHP 7+
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate a random UUIDv4. | |
* @return string UUID | |
*/ | |
function generate_uuid() | |
{ | |
$b = random_bytes(16); | |
$b[6] = chr(ord($b[6]) & 0x0f | 0x40); | |
$b[8] = chr(ord($b[8]) & 0x3f | 0x80); | |
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($b), 4)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment