まず、データを圧縮する場合は、gzip拡張機能などのphp組み込み関数を使用します。
しかし、あなたが要求したように、私はこれがPHPでどのように行われるかについての例を用意しました。これは完璧ではなく、些細な実装です。各整数のビット30と32の間のギャップを使用すると、圧縮率が向上する可能性があります。たぶんこの機能を追加するでしょう...しかし、私はバイトを優先して32ビットの符号なし整数を使用しました。損失は、バイトあたり2ビットではなく、32ビットあたり2ビットです。
最初に、関係語=> 10進数、そのコーディングテーブルを含むルックアップテーブルを準備します。
<?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
長い言葉でテストしている場合、圧縮率はもちろんさらに良くなります。
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