Skip to content

Instantly share code, notes, and snippets.

@Csardelacal
Last active August 27, 2018 07:53
Show Gist options
  • Save Csardelacal/92710a342109c4b1e76a6648957672c4 to your computer and use it in GitHub Desktop.
Save Csardelacal/92710a342109c4b1e76a6648957672c4 to your computer and use it in GitHub Desktop.
Generates valid UUID v4 in PHP.
<?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