Several ways to compress strings in PHP

WBOY
Release: 2016-07-25 08:54:43
Original
2998 people have browsed it
  1. $str = 'Compress meCompress meCompress meCompress meCompress meCompress meCompress meCompress meCompress me';
  2. echo "str".strlen($str)."n";
Copy code

Method 1 has the lowest compression rate (gzip compression algorithm) and the generated results can be written directly to the .gz file.

  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. ?>
Copy code

Method 2, compression ratio is centered – This function compress the given string using the ZLIB data format.

  1. $compressed = gzcompress('Compress me', 9);
  2. echo $compressed;
  3. ?>
Copy code

Method 3, the compression rate is tied for the highest

  1. $compressed = gzdeflate('Compress me', 9);
  2. echo $compressed;
  3. ?>
Copy code

Method 4, the compression rate is tied for the highest— Compress a string into bzip2 encoded data

  1. $str = "sample data";
  2. $bzstr = bzcompress($str, 9);
  3. echo $bzstr;
  4. ?>
Copy code

Note , there is no test to see if there is distortion in compression. Those with high compression rates should be more likely to be distorted after restoration. Use it according to the volume.

When using it, please pay attention to test the specific effect.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!