>  기사  >  백엔드 개발  >  PHP 压缩字符串的几种方法

PHP 压缩字符串的几种方法

WBOY
WBOY원래의
2016-07-25 08:54:433004검색
  1. $str = 'Compress meCompress meCompress meCompress meCompress meCompress meCompress meCompress meCompress me';
  2. echo "str".strlen($str)."\n";
复制代码

方法1,压缩率最低(gzip压缩算法) 生成结果可以直接写到.gz文件中

  1. $data = implode("", file("bigfile.txt"));
  2. $gzdata = gzencode($data, 9);
  3. $fp = fopen("bigfile.txt.gz", "w");
  4. fwrite($fp, $gzdata);
  5. fclose($fp);
  6. ?>
复制代码

方法2,压缩率居中 –This function compress the given string using the ZLIB data format.

  1. $compressed = gzcompress('Compress me', 9);
  2. echo $compressed;
  3. ?>
复制代码

方法3,压缩率并列最高

  1. $compressed = gzdeflate('Compress me', 9);
  2. echo $compressed;
  3. ?>
复制代码

方法4,压缩率并列最高 — Compress a string into bzip2 encoded data

  1. $str = "sample data";
  2. $bzstr = bzcompress($str, 9);
  3. echo $bzstr;
  4. ?>
复制代码

注意,没有测试压缩是否有失真的情况,压缩率高的应当更有可能还原后失真吧,根据体积情况使用吧。

大家在使用过程中,注意测试下具体效果。



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.