[php-src] Issue #10808: [Feature Request] Function to generate UUID
| From: | timint | Date: | Wed, 08 Mar 2023 10:31:52 +0000 |
| Subject: | [php-src] Issue #10808: [Feature Request] Function to generate UUID | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-243875@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/10808
Author: timint
### Description
Accessing different computer systems PHP users may time to time come in contact with Universally
Unique Identifiers (UUID). Not to be confused with PHP uniqid(). UUID has been around for decades
and I understand it's an accepted standard.
PHP does not have a function to generate UUIDs. I would like to see some feedback in this thread if
this is something users would like to see implemented. Maybe something like
uuid($flag=UUID_V4).
UUID Example:
fbf325c3-e809-4991-a15d-3410a3f8476d
Polyfill:
```
function guidv4($data = null) {
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
```
Wiki article:
https://en.wikipedia.org/wiki/Universally_unique_identifier
> RFC 4122 registered a [URN](https://en.wikipedia.org/wiki/URN) namespace for
> UUIDs[[1]](https://en.wikipedia.org/wiki/Universally_unique_identifier#cite_note-RFC_4122-1) and
> recapitulated the earlier specifications, with the same technical content.[[citation
> needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed)] When in July 2005 RFC 4122 was
> published as a proposed [IETF](https://en.wikipedia.org/wiki/IETF) standard, the
> [ITU](https://en.wikipedia.org/wiki/ITU) had also standardized UUIDs, based on the previous
> standards and early versions of RFC 4122.