PHP

Validate UUID (any version)

admin by @admin ADMIN
29m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Match the canonical 8-4-4-4-12 hex format, optionally pinning to a specific UUID version (1-5). Case-insensitive.
PHP
Raw
<?php
function isValidUuid(string $uuid, ?int $version = null): bool {
    $uuid = trim($uuid);
    if ($version === null) {
        return (bool)preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i', $uuid);
    }
    return (bool)preg_match(
        '/^[0-9a-f]{8}-[0-9a-f]{4}-' . $version . '[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i',
        $uuid
    );
}

var_dump(isValidUuid('550e8400-e29b-41d4-a716-446655440000'));    // true (v4)
var_dump(isValidUuid('550e8400-e29b-41d4-a716-446655440000', 4)); // true
var_dump(isValidUuid('not-a-uuid'));                              // false
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.