Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - check for valid guid

Tags:

php

guid

I have a mssql database in which my primary keys are GUIDs. I am working on a web interface for inserting some data. I need a GUID, generated by php. I am using com_create_guid() function. So, before trying to insert I want to ensure that my parameters are valid. I can not find a way to check if a string(com_create_guid() returns string) is a valid GUID.

like image 662
Svetlozar Angelov Avatar asked Aug 10 '09 06:08

Svetlozar Angelov


People also ask

How do I know if a GUID is valid?

A GUID (in hex form) need not contain any alpha characters, though by chance it probably would. If you are targeting a GUID in hex form, you can check that the string is 32-characters long (after stripping dashes and curly brackets) and has only letters A-F and numbers.

What characters are valid in a GUID?

The valid GUID (Globally Unique Identifier) must specify the following conditions: It should be a 128-bit number. It should be 36 characters (32 hexadecimal characters and 4 hyphens) long. It should be displayed in five groups separated by hyphens (-).

What is GUID PHP?

Description ¶ com_create_guid(): string|false. Generates a Globally Unique Identifier (GUID). A GUID is generated in the same way as DCE UUID's, except that the Microsoft convention is to enclose a GUID in curly braces.

Can GUID have special characters?

Theoretically you can display each byte as an extended ASCII character (255 characters), which would allow you to save a GUID as a 16 character length string.


2 Answers

There are a few rules that should be imposed on the UUID/GUID pattern.

  • The only valid letters are a, b, c, d, e, and f.
  • 0-9 can be replaced with the digit pattern \d
  • GUIDs are often case insensitive.
  • You either have {both brackets}, or none at all.

Simplified patterns

  • hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh
  • {hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh}

Expression:

var_dump( preg_match("/^(\{)?[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}(?(1)\})$/i", $guid) ? "ok", "not ok"); 

Translation:

  1. / beginning of expression
  2. ^ beginning of string
  3. (\{)? optional opening bracket {
  4. [a-f\d]{8} 8 hex characters hhhhhhhh
  5. (-[a-f\d]{4}) 4 hex characters proceeded by dash -hhhh
  6. {4} previous pattern repeated 4 times
  7. [a-f\d]{8} 8 hex characters hhhhhhhh
  8. (?(1)\}) if first pattern was present {, then match closing tag }
  9. $ end of string
  10. / close expression
  11. i ignore case sensitivity
like image 86
Lewie Avatar answered Sep 19 '22 03:09

Lewie


Considering a GUID is defined as something like this : "A98C5A1E-A742-4808-96FA-6F409E799937" (from what the wikipedia page says)

I suppose using a regex like this one would do :

$guid = 'A98C5A1E-A742-4808-96FA-6F409E799937'; if (preg_match('/^\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\}?$/', $guid)) {   var_dump('ok'); } else {   var_dump('not ok'); } 

It will match for

  • 8 characters (both letters and numbers)
  • 4 characters
  • 4 characters
  • 4 characters
  • 12 characters

Each set of characters being separated by a '-'


Considering you're using com_create_guid, the regex check for optionnals } and { arround the guid, which means this would display 'ok' too :

$guid = '{A98C5A1E-A742-4808-96FA-6F409E799937}'; if (preg_match('/^\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\}?$/', $guid)) {   var_dump('ok'); } else {   var_dump('not ok'); } 
like image 44
Pascal MARTIN Avatar answered Sep 23 '22 03:09

Pascal MARTIN