Skip to content

Instantly share code, notes, and snippets.

@Joel-James
Last active March 6, 2024 22:26
Show Gist options
  • Save Joel-James/3a6201861f12a7acf4f2 to your computer and use it in GitHub Desktop.
Save Joel-James/3a6201861f12a7acf4f2 to your computer and use it in GitHub Desktop.
Check if UUID is in valid format
<?php
/**
* Check if a given string is a valid UUID
*
* @param string $uuid The string to check
* @return boolean
*/
function isValidUuid( $uuid ) {
if (!is_string($uuid) || (preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $uuid) !== 1)) {
return false;
}
return true;
}
@eusonlito
Copy link

eusonlito commented Jan 30, 2024

One line :)

/**
 * Check if a given string is a valid UUID
 * 
 * @param   mixed  $uuid   The string to check
 * @return  boolean
 */
function isValidUuid(mixed $uuid): bool
{    
    return is_string($uuid) && preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $uuid);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment