title: PHP LZW Binary Decompression Function
body: <p>I've been looking on the internets and couldn't find an LZW decompression implementation in PHP that works with the data outputted by these javascript functions:</p>
  
  <pre><code>function lzw_encode(s) {
      var dict = {};
      var data = (s + "").split("");
      var out = [];
      var currChar;
      var phrase = data[0];
      var code = 256;
      for (var i=1; i&lt;data.length; i++) {
          currChar=data[i];
          if (dict[phrase + currChar] != null) {
              phrase += currChar;
          }
          else {
              out.push(phrase.length &gt; 1 ? dict[phrase] : phrase.charCodeAt(0));
              dict[phrase + currChar] = code;
              code++;
              phrase=currChar;
          }
      }
      out.push(phrase.length &gt; 1 ? dict[phrase] : phrase.charCodeAt(0));
      for (var i=0; i&lt;out.length; i++) {
          out[i] = String.fromCharCode(out[i]);
      }
      return out.join("");
  }
  
  function lzw_decode(s) {
      var dict = {};
      var data = (s + "").split("");
      var currChar = data[0];
      var oldPhrase = currChar;
      var out = [currChar];
      var code = 256;
      var phrase;
      debugger;
      for (var i=1; i&lt;data.length; i++) {
          var currCode = data[i].charCodeAt(0);
          if (currCode &lt; 256) {
              phrase = data[i];
          }
          else {
             phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
          }
          out.push(phrase);
          currChar = phrase.charAt(0);
          dict[code] = oldPhrase + currChar;
          code++;
          oldPhrase = phrase;
      }
      return out.join("");
  }
  </code></pre>
  
  <p>I really just need a decompression algorithm in PHP that can work with the compression javascript function above.</p>
  
  <p>The lzw_encode function above encodes "This is a test of the compression function" as <b>"This Ă a test ofĈhe comprĊsion functěn"</b></p>
  
  <p>The libraries I've found are either buggy (http://code.google.com/p/php-lzw/) or don't take input of UTC characters.</p>
  
  <p>Any help would be greatly appreciated,</p>
  
  <p>Thanks!</p>