Skip to content

Instantly share code, notes, and snippets.

@Joel-James
Last active March 6, 2024 22:26
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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;
}
@ellisgl
Copy link

ellisgl commented Sep 17, 2019

/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i

@edi
Copy link

edi commented Oct 12, 2019

@ellisgl method works just fine. Less overhead.

@ellisgl
Copy link

ellisgl commented Oct 12, 2019

Just another way to write it. Would be interesting to come up with a way to bench mark mupliple regexs...

@edi
Copy link

edi commented Oct 12, 2019

@ellisgl I actually wanted to mention your method works better lol :)

I ran both 10K times in PHP 7.3.10 //=> 22% faster than the first one.

@ellisgl
Copy link

ellisgl commented Oct 12, 2019

Wow, I didn't expect that!

@sivolobov
Copy link

@ellisgl's method can give the false positive answer.
If you look at https://tools.ietf.org/html/rfc4122#section-4.1 you can find that not every 128-bits sequence represented in string with hyphens in right places is valid UUID.

@ellisgl
Copy link

ellisgl commented Dec 10, 2019

@sivolobov should match hex string of 8, 4, 4, 4, 12...

@ludapesi
Copy link

ludapesi commented Apr 8, 2020

Why not just return (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid) === 1);
It's return false isn't a string and the string need to be a UUID.
This means less processing even if it's just a few cycles.

@ellisgl
Copy link

ellisgl commented Apr 8, 2020

Could leave the '=== 1' off too.

@ludapesi
Copy link

ludapesi commented Apr 9, 2020

@ellisgl without comparing don't have a Boolean return.
preg_match returns 0, 1, false

@ellisgl
Copy link

ellisgl commented Apr 9, 2020

Ah, that's right.. ugh.

@impressto
Copy link

Thank you ellisgl. Your method works perfectly for me.

@ellisgl
Copy link

ellisgl commented Jul 25, 2020

@impressto You're welcome.

@cakebake
Copy link

/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i

@ellisgl works wonderfully - thank you

@ellisgl
Copy link

ellisgl commented Jan 29, 2021

@cakebake You're welcome.

@swatchion
Copy link

great gist, I come up with this
return $uuid && preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $uuid);

@rafaelgamezdiaz
Copy link

/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i

Excelent!! It works fine.

@Thiagothims
Copy link

Perfect!

@Joel-James
Copy link
Author

Updated based on @ellisgl suggestion

@ellisgl
Copy link

ellisgl commented Jan 5, 2024

🤩

@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