php对字符串压缩,主要用到二个函数,分别是:gzcompress() 和gzuncompress()。
下面是一个具体的例子,供参考:
<?php /** * 功能:字符串压缩 * 函数gzcompress() 和gzuncompress()的应用实例 * edit by www.jb200.com */ $string = “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut elit id mi ultricies adipiscing. Nulla facilisi. Praesent pulvinar, sapien vel feugiat vestibulum, nulla dui pretium orci, non ultricies elit lacus quis ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam pretium ullamcorper urna quis iaculis. Etiam ac massa sed turpis tempor luctus. Curabitur sed nibh eu elit mollis congue. Praesent ipsum diam, consectetur vitae ornare a, aliquam a nunc. In id magna pellentesque tellus posuere adipiscing. Sed non mi metus, at lacinia augue. Sed magna nisi, ornare in mollis in, mollis sed nunc. Etiam at justo in leo congue mollis. Nullam in neque eget metus hendrerit scelerisque eu non enim. Ut malesuada lacus eu nulla bibendum id euismod urna sodales. “; $compressed = gzcompress($string); echo “Original size: “. strlen($string).”n”; /* 输出原始大小 Original size: 800 */ echo “Compressed size: “. strlen($compressed).”n”; /* 输出压缩后的大小 Compressed size: 418 */ // 解压缩 $original = gzuncompress($compressed); ?>
说明:
以上代码,产生几乎50% 压缩比率。
另外,在php中对字符串的压缩,还可以使用 gzencode() 和 gzdecode() 函数来压缩,与函数gzcompress() 和gzuncompress()的不同,仅在于压缩算法不同。