假设我想在PHP中存储8个单词的序列,我不想使用压缩。
由于只有8个单词,我可以为每个单词分配一个二进制值,然后将这些二进制值存储在一个文件中,而不是ascii单词。
可能的二进制值为:
000, 001, 010, 011, 100, 101, 110, 111
解析会更有效率,因为:(1)每个单词现在大小相同,(2)它占用的空间要少得多。
我的问题是:
我怎样才能在PHP中执行此操作?如何将二进制值分配给某个东西,然后将其写入文件(将这些位写入我想要的位置),然后再读回来?
我想这样做的原因是创建一个有效的索引系统。
答案 0 :(得分:4)
首先,如果你想压缩数据,请使用php内置函数,就像gzip扩展名一样。
但正如您所要求的那样,我已经准备了一个如何在PHP中完成此操作的示例。它并不完美,只是一个简单的实现。如果我使用每个整数的位30和32之间的间隙,压缩率可能会更好。也许会添加这个功能...但是我使用32位无符号整数来支持字节,因为损失是每32位2位而不是每字节2位。
首先,我们准备包含关系词=>的查找表。十进制数,其编码表:
<?php
// coding table
$lookupTable = array (
// 'word0' => chr(0), // reserved for 0 byte gap in last byte
'word1' => chr(1),
'word2' => chr(2),
'word3' => chr(3),
'word4' => chr(4),
'word5' => chr(5),
'word6' => chr(6),
// reserve one word for white space
' ' => chr(7)
);
然后是压缩函数:
/**
*
*/
function _3bit_compress($text, $lookupTable) {
echo 'before compression : ' . strlen($text) . ' chars', PHP_EOL;
// first step is one byte compression using the lookup table
$text = strtr($text, $lookupTable);
echo 'after one byte per word compression : ' . strlen($text) . ' chars', PHP_EOL;
$bin = ''; // the result
$carrier = 0; // 32 bit usingned int can 'carry' 10 words in 3 bit notation
for($c = 0; $c < strlen($text); $c++) {
$triplet = $c % 10;
// every 30 bits we add the 4byte unsigned integer to $bin.
// please read the manual of pack
if($triplet === 0 && $carrier !== 0) {
$bin .= pack('N', $carrier);
$carrier = 0;
}
$char = $text[$c];
$carrier <<= 3; // make space for the the next 3 bits
$carrier += ord($char); // add the next 3 bit pattern
// echo $carrier, ' added ' . ord($char), PHP_EOL;
}
$bin .= pack('N', $carrier); // don't forget the remaining bits
echo 'after 3 bit compression : ' . strlen($bin) . ' chars', PHP_EOL;
return $bin;
}
减压功能:
/**
*
*/
function _3_bit_uncompress($compressed, $lookupTable) {
$len = strlen($compressed);
echo 'compressed length: : ' . $len . ' chars', PHP_EOL;
$i = 0;
$tmp = '';
$text = '';
// unpack string as 4byte unsigned integer
foreach(unpack('N*', $compressed) as $carrier) {
while($i < 10) {
$code = $carrier & 7; // get the next code
// echo $carrier . ' ' . $code, PHP_EOL;
$tmp = chr($code) . $tmp;
$i++;
$carrier >>= 3; // shift forward to the next 3 bits
}
$i = 0;
$text = $text . $tmp;
$tmp = '';
}
// reverse translate from decimal codes to words
return strtr($text, array_flip($lookupTable));
}
现在是测试功能的时候了:)
$original = <<<EOF
word1 word2 word3 word4 word5 word6 word1 word3 word3 word2
EOF;
$compressed = _3bit_compress($original, $lookupTable);
$restored = _3_bit_uncompress($compressed, $lookupTable);
echo 'compressed size: ' . round(strlen($compressed) * 100 / strlen($original), 2) . '%', PHP_EOL;
echo 'Message before compression : ' . $original, PHP_EOL;
echo 'Message after decompression : ' . $restored, PHP_EOL;
该示例应该为您提供:
before compression : 60 chars
after one byte per word compression : 20 chars
after 3 bit compression : 8 chars
compressed length: : 8 chars
compressed size: 13,33%
Message before compression : word1 word2 word3 word4 word5 word6 word1 word3 word3 word2
Message after decompression : word1 word2 word3 word4 word5 word6 word1 word3 word3 word2
如果我们使用loooong单词进行测试,压缩率当然会变得更好:
before compression : 112 chars
after one byte per word compression : 16 chars
after 3 bit compression : 8 chars
compressed length: : 8 chars
compressed size: 7,14%
Message before compression : wooooooooord1 wooooooooord2 wooooooooord2 wooooooooord3 wooooooooord1 wooooooooord2 wooooooooord2 wooooooooord3
Message after decompression : wooooooooord1 wooooooooord2 wooooooooord2 wooooooooord3 wooooooooord1 wooooooooord2 wooooooooord2 wooooooooord3