What is UUID? A universally unique identifier (UUID) is an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE).

A UUID is a 16-byte (128-bit) number. The number of theoretically possible UUIDs is therefore about 3 × 1038. In its canonical form, a UUID is represented by 32 hexadecimal digits, displayed in 5 groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 digits and 4 hyphens). For example:

550e8400-e29b-41d4-a716-446655440000

UUID is very useful while developing PHP applications. It is also increases security of your application while using UUID instead of ID number. I have created couple of methods how to create UUID and how to validate it in PHP. Enjoy working with UUID!

class Uuid
{
 /**
  * creates a universally unique identifier (UUID)
  *
  * @return string uuid
  * @access public
  * @static
  */
 public static function create_uuid()
 {
  $uuid =  md5(uniqid());
  return sprintf('%s-%s-%s-%s-%s',
         substr($uuid, 0, 8),
         substr($uuid, 8, 4),
         substr($uuid, 12, 4),
         substr($uuid, 16, 4),
         substr($uuid, 20, 12)
        );
 }
 
 /**
  * checks and returns true/false if given string is uuid
  *
  * @return bool true/false if given string is uuid
  * @access public
  * @static
  */
 public static function is_uuid($uuid)
 {
  return (boolean) preg_match('/^\{?[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}\}?$/i', trim((String) $uuid));
 }
 
}

Dima Svirid

Software architect, JAVA, Spring, Hibernate, AngularJs, Backbone, MongoDB, Oracle. CTO and Co-Founder of Homeadnet.com

More Posts

Follow Me:

Tagged with: