Skip to content

Instantly share code, notes, and snippets.

@lsloan
Created May 12, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsloan/6ab7d4a40e1410e1c93a to your computer and use it in GitHub Desktop.
Save lsloan/6ab7d4a40e1410e1c93a to your computer and use it in GitHub Desktop.
A method for creating GUIDs for PHP, even if com_create_guid() isn't available.
private function getNewGUID($wrapWithBraces = false) {
if (function_exists('com_create_guid')) {
return com_create_guid();
} else {
mt_srand((double)microtime() * 10000); //optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$uuid = ($wrapWithBraces ? '{' : '')
. substr($charid, 0, 8) . '-'
. substr($charid, 8, 4) . '-'
. substr($charid, 12, 4) . '-'
. substr($charid, 16, 4) . '-'
. substr($charid, 20, 12)
. ($wrapWithBraces ? '}' : '');
return $uuid;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment