Last active
August 27, 2018 07:53
-
-
Save Csardelacal/92710a342109c4b1e76a6648957672c4 to your computer and use it in GitHub Desktop.
Generates valid UUID v4 in PHP.
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 | |
function uuid() { | |
$data = random_bytes(16); | |
$data[6] = chr((ord($data[6]) | 0xC0) & 0x4F); | |
$data[8] = chr((ord($data[8]) | 0xC0) & 0xBF); | |
$str = bin2hex($str); | |
return sprintf('%s-%s-%s-%s-%s', substr($str, 0, 8), substr($str, 8, 4), substr($str, 12, 4), substr($str, 16, 4), substr($str, 20)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment