Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

ShortGUID

4.75/5 (4 votes)
30 Apr 2012CPOL1 min read 39.6K  
A set of functions to get slightly shorter unique Guids

Introduction

Here is a set of functions to shorten GUIDs in C# and PHP.

It is a reversible algorithm that transform a guid of 36 characters to 22 characters.
 

Background

GUIDs are a very useful tool for your web and application developers, but a little bit verbose.

The small function below lets you shave 30% off the guid string size while keeping the entire data.

This is not rocket science, I use the standard: Base64 with URL and Filename Safe Alphabet (RFC 4648 'base64url' encoding) without the end padding.

How does it work.

A normal guid uses the characters '0' to '9', 'A' to 'F'. This is a range of 16 different characters. It also have generally 4 dashes that contain no data.

  • A standard guid contains 128 bits of data and is generally displayed as string  of length 36.

Fortunately, we can use more characters.

This algorithm uses the characters '0' to '9', 'a' to 'z', 'A' to 'Z' and also '-' et '_'. This is a range of 64 characters.

Using a range of 64 different characters, to store 128 bits we only need a string of length 22.

Therefore a shortGuid is 22 characters long

Using the Code

The ToShortString method produces a short string like this one: U1ZBOzMa-0eV_4gFv965cQ

C#
String shortString = someGuid.ToShortString();

Then the ParseShortGuid:

C#
Guid parsedGuid =  ParseShortGuid("U1ZBOzMa-0eV_4gFv965cQ"); 

The Main C# Functions

C#
private static string ToShortGuid(this Guid newGuid)
{
    string modifiedBase64 = Convert.ToBase64String(newGuid.ToByteArray())
        .Replace('+', '-').Replace('/', '_') // avoid invalid URL characters
        .Substring(0, 22);
    return modifiedBase64;
}

private static Guid ParseShortGuid(string shortGuid)
{
    string base64 = shortGuid.Replace('-', '+').Replace('_', '/') + "==";
    Byte[] bytes = Convert.FromBase64String(base64);
    return new Guid(bytes);
} 

The Main PHP Functions

These are the equivalent functions in PHP.

PHP
function ParseShortGuid($a)
{
   $a=str_replace('_','/',str_replace('-','+',$a))."==";
   $a=base64_decode($a);
   if ($a==false || strlen($a)<16) return NULL;
   $a= bin2hex($a);
   $a = substr($a,6,2).substr($a,4,2).substr($a,2,2).substr($a,0,2)
      .'-'.substr($a,10,2).substr($a,8,2)
      .'-'.substr($a,14,2).substr($a,12,2)
      .'-'.substr($a,16,4)
      .'-'.substr($a,20,12);
   return $a;
}    

ToShortGuid uses the hex2bin function if you have a recent PHP but will fallback if it is not available.

PHP
function ToShortGuid($a)
{
   $a=str_replace('-','',str_replace('{','',str_replace('}','',$a)));
   $a = substr($a,6,2).substr($a,4,2).substr($a,2,2).substr($a,0,2)
     .substr($a,10,2).substr($a,8,2)
     .substr($a,14,2).substr($a,12,2)
     .substr($a,16,4)
     .substr($a,20,12);
   if(function_exists('hex2bin')) $a= hex2bin($a);
   else
   {
      $bin = '';
      for ($i = 0; $i < strlen($a); $i += 2) {
        $bin .= chr(hexdec($a{$i}.$a{($i + 1)}));
      }
      $a = $bin;
   }
   $a=base64_encode($a);
   $a=str_replace('/','_',str_replace('+','-',$a));
   $a=substr($a,0,22);
   return $a;
}      

Points of Interest

It would be nice to post other languages.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)