我一直在寻找互联网,并且在
PHP中找不到与这些javascript函数输出的数据一起使用的LZW解压缩实现:
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<data.length; i++) { currChar=data[i]; if (dict[phrase + currChar] != null) { phrase += currChar; } else { out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); dict[phrase + currChar] = code; code++; phrase=currChar; } } out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); for (var i=0; i<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<data.length; i++) { var currCode = data[i].charCodeAt(0); if (currCode < 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(""); }
我真的只需要PHP中的解压缩算法,它可以使用上面的压缩javascript函数.
上面的lzw_encode函数将“这是对压缩函数的测试”编码为“这是对于编译功能的测试”
我找到的图书馆要么是错误的(http://code.google.com/p/PHP-lzw/),要么不要输入UTC字符.
任何帮助将不胜感激,
谢谢!
我已经移植并测试了它给你PHP:
原文链接:https://www.f2er.com/php/136770.htmlfunction lzw_decode($s) { mb_internal_encoding('UTF-8'); $dict = array(); $currChar = mb_substr($s,1); $oldPhrase = $currChar; $out = array($currChar); $code = 256; $phrase = ''; for ($i=1; $i < mb_strlen($s); $i++) { $currCode = implode(unpack('N*',str_pad(iconv('UTF-8','UTF-16BE',mb_substr($s,$i,1)),4,"\x00",STR_PAD_LEFT))); if($currCode < 256) { $phrase = mb_substr($s,1); } else { $phrase = $dict[$currCode] ? $dict[$currCode] : ($oldPhrase.$currChar); } $out[] = $phrase; $currChar = mb_substr($phrase,1); $dict[$code] = $oldPhrase.$currChar; $code++; $oldPhrase = $phrase; } var_dump($dict); return(implode($out)); }