Skip to content

Instantly share code, notes, and snippets.

@tdomarkas
Created January 28, 2015 21:53
Show Gist options
  • Save tdomarkas/c5fbc10385ae004cbde6 to your computer and use it in GitHub Desktop.
Save tdomarkas/c5fbc10385ae004cbde6 to your computer and use it in GitHub Desktop.

uuid

uuid is a PHP module for generating UUIDs. It can be installed from PECL with the following command:

sudo pecl install uuid

Functions

string uuid_create(int $type)

Creates a UUID based on a passed type:

  • UUID_TYPE_DEFAULT (0)
  • UUID_TYPE_TIME (1)
  • UUID_TYPE_RANDOM (4)
$uuid = uuid_create(UUID_TYPE_RANDOM);
echo $uuid; // => "a04e572a-0612-4519-8450-c6f64adb0be1"
  • UUID_TYPE_INVALID (-42)
  • UUID_TYPE_DCE (4)
  • UUID_TYPE_NAME (1)

bool uuid_is_valid(string $uuid)

Tells if provided UUID is valid.

uuid_is_valid('a04e572a-0612-4519-8450-c6f64adb0be1'); // => true
uuid_is_valid('foo-bar'); // => false

bool uuid_compare(string $uuid, string $another_uuid)

Tell if two UUIDs match.

$uuid = 'a04e572a-0612-4519-8450-c6f64adb0be1';
$another_uuid = '98ed652b-f6a2-4c68-a8c6-815f50657a57';

uuid_compare($uuid, $uuid); // => true
uuid_compare($uuid, $another_uuid); // => false

bool uuid_is_null(string $uuid)

Tells if a provided UUID is of null type (UUID_TYPE_NULL (-1)). This kind of UUID contains all zeros instead of other hexadecimal values.

$null_uuid = '00000000-0000-0000-0000-000000000000';
uuid_is_null($null_uuid); // => true

$uuid = 'a04e572a-0612-4519-8450-c6f64adb0be1';
uuid_compare($uuid, $uuid); // => false

int uuid_type(string $uuid)

Returns the type of the provided UUID.

$time_uuid = '1ed0df7a-a009-11e3-b19b-2b3fc91c18ca';
uuid_type($time_uuid); // => 2

$rand_uuid = 'a04e572a-0612-4519-8450-c6f64adb0be1';
uuid_type($rand_uuid); // => 4

int uuid_variant(string $uuid)

Returns a UUID variant which can be any of these values:

  • UUID_VARIANT_DCE (1) - Unix-like UUID variant
  • UUID_VARIANT_MICROSOFT (2) - Windows UUID variant
  • UUID_VARIANT_OTHER (3) - Other OS UUID variant
$uuid = '1ed0df7a-a009-11e3-b19b-2b3fc91c18ca';
uuid_variant($uuid); // => 1

int uuid_time(string $uuid)

Returns a Unix timestamp from a UUID of time type.

$time_uuid = 'b8674158-a00c-11e3-aa3f-ab179b870a60';
uuid_time($uuid); // => 1393546224

$rand_uuid = '1ed0df7a-a009-11e3-b19b-2b3fc91c18ca';
uuid_time($uuid); // => false

string uuid_mac(string $uuid)

Returns a MAC address from UUID. No code samples available yet.

string uuid_parse(string $uuid)

Translates a UUID to 16 byte binary.

uuid_parse('06b46301-0a4d-4096-afb3-270de248e46b'); // => "��c� M@���' �H�k"

string uuid_unparse(string $uuid_binary)

Translates a parsed UUID to its original form. See uuid_parse.

uuid_unparse('�c� M@���' �H�k'); // => "06b46301-0a4d-4096-afb3-270de248e46b�"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment