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.
views:
776answers:
3
A:
Here are a bunch of regex's for different versions of GUIDs: http://www.roscripts.com/PHP_regular_expressions_examples-136.html
Zed
2009-08-10 06:37:56
+3
A:
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');
}
Pascal MARTIN
2009-08-10 06:39:51
The regex can be simplified:[A-Z0-9]{8}-(?:[A-Z0-9]{4}-){3}[A-Z0-9]{12}
Alix Axel
2009-08-10 10:38:34
it can be made shorter than what I proposed, indeed :-) But I think I'd still go with the "long" version, which is "simpler/quickier" to understand when you have to read it -- it won't change much, but as much as regex are a powerful tool, they are often hard to understand for those who don't know them well...
Pascal MARTIN
2009-08-10 10:57:37
Aren't GUIDs hexadecimal? So why match on A-Z instead of A-F? Or am I missing something here?
kander
2010-08-17 10:31:26
+3
A:
Their are a few rules that should be imposed on the UUID/GUid pattern.
- The only valid letters are a, b, c, d, 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:
- / beginning of expression
- ^ beginning of string
- (\{)? optional opening bracket {
- [a-f\d]{8} 8 hex characters hhhhhhhh
- (-[a-f\d]{4}) 4 hex characters proceeded by dash -hhhh
- {4} previous pattern repeated 4 times
- [a-f\d]{8} 8 hex characters hhhhhhhh
- (?(1)\}) if first pattern was present {, then match closing tag }
- $ end of string
- / close expression
- i ignore case sensitivity
Lewis Moten
2009-10-04 02:36:54