Skip to content

Core\Uuid::tryParse

Core\Uuid::tryParse(string $s): ?Core\UuidCore\Uuid::tryParse(
    string $s,
): ?Core\Uuid

Reads a text as a UUID, like Core\Uuid::parse, and returns null when the text is not a UUID. It never throws an error, so it is the way to check whether a text is a UUID, such as an ID that arrives in a URL or a form.

The text must be 32 hexadecimal digits in groups of 8, 4, 4, 4 and 12, joined by hyphens, such as f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4. Upper case and lower case letters are both allowed. The 32 digits without hyphens, a UUID in {} braces and a UUID after urn:uuid: all give null. The nil UUID (all zeros) and the max UUID (all f) are valid.

The examples below read a valid UUID, show which forms give null, and check the ID in a request path before the program looks anything up.

Parameters

  • $sstringThe text to read. It must be 32 hexadecimal digits in groups of 8, 4, 4, 4 and 12, joined by hyphens. Upper case and lower case letters are both allowed.

Return value

Returns ?Core\Uuid. The Uuid that the text gives, or null when the text is in any other form. The 32 digits without hyphens, a UUID in {} braces and a UUID after urn:uuid: all give null.

Examples

Read a uuid or null

<?nvs
// Shows how to read a text as a UUID when the text may not be one.
// The text is a UUID, so `Core\Uuid::tryParse` returns a `Uuid`.
// This prints f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4.
?Core\Uuid $id = Core\Uuid::tryParse('F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4');
echo $id?->toString() ?? 'not a UUID', "\n";
// The text is not a UUID, so the result is `null`. This prints not a UUID.
?Core\Uuid $other = Core\Uuid::tryParse('order-1042');
echo $other?->toString() ?? 'not a UUID', "\n";
Output
f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4
not a UUID

Forms that give null

<?nvs
// Shows which ways to write a UUID are accepted.
array<string> $texts = [
'f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4',
'f9168c5eceb24faab6bf329bf39fa1e4',
'{f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}',
'urn:uuid:f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4',
'00000000-0000-0000-0000-000000000000',
];
// Only the form with four hyphens is accepted. The nil UUID has only zeros and is valid.
// This prints yes, no, no, no and yes.
foreach ($texts as string $text) {
echo Core\Uuid::tryParse($text) == null ? 'no' : 'yes', ' ', $text, "\n";
}
Output
yes f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4
no f9168c5eceb24faab6bf329bf39fa1e4
no {f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}
no urn:uuid:f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4
yes 00000000-0000-0000-0000-000000000000

Check the id in a request path

<?nvs
// Shows how to check the ID in a request path before the program looks up the order.
class Orders
{
// Returns the answer for a path such as /orders/<id>.
public static function show(string $path): string
{
?Core\Uuid $id = Core\Uuid::tryParse(Core\Str::slice($path, 8));
if ($id == null) {
return '404 no such order';
}
return '200 order ' . $id->toString();
}
}
// This prints 200 order f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4.
echo Orders::show('/orders/f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4'), "\n";
// The path has no UUID, so no lookup happens. This prints 404 no such order.
echo Orders::show('/orders/1042'), "\n";
Output
200 order f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4
404 no such order