PHP generate UUIDv4 PHP 229
By IST on 2nd December 2019 10:02:46 PM

  1. function guidv4($data = null) {
  2.   $data = $data ?? random_bytes(16);
  3.   assert(strlen($data) == 16);
  4.   $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  5.   $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  6.   return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  7. }
  8.  
  9. $data = openssl_random_pseudo_bytes(16);
  10. echo guidv4($data) . "\n";
  11. echo guidv4() . "\n";
Raw Paste

Login or Register to edit or fork this paste. It's free.