PHP 压缩字符串方法
2
根据压缩算法的不同,php提供了如下几种方法:
$str = 'Compress meCompress meCompress meCompress meCompress meCompress meCompress meCompress meCompress me';
echo "str".strlen($str)."\n";
//压缩率最低(gzip压缩算法) 生成结果可以直接写到.gz文件中
<?php
$data = implode("", file("bigfile.txt"));
$gzdata = gzencode($data, 9);
$fp = fopen("bigfile.txt.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
?>
$data = implode("", file("bigfile.txt"));
$gzdata = gzencode($data, 9);
$fp = fopen("bigfile.txt.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
?>
//压缩率居中 –This function compress the given string using the ZLIB data format.
<?php
$compressed = gzcompress('Compress me', 9);
echo $compressed;
?>
$compressed = gzcompress('Compress me', 9);
echo $compressed;
?>
//压缩率并列最高
<?php
$compressed = gzdeflate('Compress me', 9);
echo $compressed;
?>
$compressed = gzdeflate('Compress me', 9);
echo $compressed;
?>
//压缩率并列最高 — Compress a string into bzip2 encoded data
<?php
$str = "sample data";
$bzstr = bzcompress($str, 9);
echo $bzstr;
?>
$str = "sample data";
$bzstr = bzcompress($str, 9);
echo $bzstr;
?>
我并没有测试压缩是否有失真的情况,压缩率高的应当更有可能还原后失真吧,根据体积情况使用吧。
转自:http://www.linuxpig.com/2010/08/phpyasuozifuchuanfangfa/
转自:http://www.linuxpig.com/2010/08/phpyasuozifuchuanfangfa/