Fixed version note
You will notice
on this old version that if you encode and decode the text it will not be the same.
After a while of debugging I finally noticed (the algorithm looked perfect!) that the problem was caused by the
php function in_array that I used to determine if word+x is in the dictionary (see LZW algorithm).
The following code demonstrates the problem:
$myarray = array(" 1", "a");
echo in_array("1", $myarray); //echo in_array("1", $myarray, true); will work correctly
The output is 1 (true) - it seems that in_array trims the string " 1", so that it equates to "1". The fix was simple - use the strict paramater of in_array and it will also check the types. :-)