9951 explained code solutions for 126 technologies


php-regexHow to match a GUID using regex in PHP?


A GUID (Globally Unique Identifier) is a 128-bit number used to identify resources in a unique way. To match a GUID using regex in PHP, you can use the following code:

$regex = '/^\{?[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}?$/';

if (preg_match($regex, $guid)) {
    echo "Matched!";
}

The code above uses the following parts:

  • $regex: The regex pattern used to match the GUID. It consists of 8 hexadecimal characters, followed by a hyphen, followed by 3 groups of 4 hexadecimal characters, followed by a hyphen, followed by 12 hexadecimal characters.
  • preg_match(): The PHP function used to match the regex pattern against the given string. It takes two parameters: the regex pattern and the string to match against.
  • $guid: The string to match against.

If the string matches the regex pattern, the preg_match() function will return true and the output will be Matched!.

Helpful links

Edit this code on GitHub