Hello Guys,
The lzw functions are very nice and fast. i want to implement this in a auto js compression script in php (with cache) but there is one problem: chr seems to act differently when comparing to fromCharCode in javascript, they give not the same results when charcode is above 256. So what can i do about this? Her is my translation of lzw_encode():
function lzw_encode($s)
{
$dict = array();
$data = "".str_replace( "\r", "", $s );
//print_r( $data ); die;
$out = array();
$currChar = 0;
$phrase = $data{0};
$code = 256;
$c = strlen($data);
for ($i=1; $i<$c; $i++){
$currChar=$data{$i};
if($dict[$phrase.$currChar] != null) {
$phrase.=$currChar;
}
else {
$out[]=(strlen($phrase) > 1 ? $dict[$phrase] : ord($phrase{0}));
//print( "'".((strlen($phrase) > 1) ? $dict[$phrase] : ord($phrase{0}))."'\n");
$dict[$phrase.$currChar] = $code;
$code++;
$phrase=$currChar;
}
}
$out[]=(strlen($phrase) > 1)? $dict[$phrase] : ord($phrase{0});
$c = count($out);
for($i=0; $i<$c; $i++) {
$out[$i] = chr($out[$i]); // <-- here it is
}
return implode("",$out);
}
Thank you very much for your reply.
Greetz,
Erwinus