base64 encode and decode to fix strings

I was working on a project where I would store a serialized blob of data into a DB.  After some time the requirements changed we now needed to store HTML encoded and other special characters such as single and double quotes.  The characters – particularly the single and double quotes would not serialize properly.  You would end up with the character in the serialized string which would cause all kinds of problems trying to store them into the DB.

After some researching online I found detail on wrapping the serialized object inside of a base64_encoding call.  So that is what I pursued.  In essence you first serialize the text – then you base 64 encode is.  When you want the data out you first base64 decode it, then you unserialize it.

Here is an example code snippet assuming that you put these function into a Util class.

class Util {
  //Standard Packing Algorithm for objects
  public static function pack($object){
    return base64_encode(serialize($object));
  //Standard UnPacking Algorithm for objects
  public static function unpack($object){
    $object = unserialize(base64_decode($object));
    return $object;


You will notice that I called these functions pack and unpack. I also made them static. Depending on your implementation you may put them inside the object class you are trying to encode and decode. For my specific project I wanted the same functionality for all objects across the project – so a Util class made the most sense.

Another interesting performance side effect of this approach. At first glance you may say to yourself… hmmm this will put a lot of pressure on the Web Nodes doing the packing and unpacking. While this is true, you are also reducing the amount of stress being sent over the pipe to your DB point. There is less data going in and out of your DB and thus your queries end up performing a lot faster. When I have worked on very large scale applications that have a web node pool of say 200+ and a DB shard count of 32+ we would also prefer to put more load on the web nodes then on the DB’s. The system as a whole just ran a lot smoother.

Posted in , .

Leave a Reply

Your email address will not be published. Required fields are marked *