Best way to compress string in PHP

I am compressing an array using gzcompress (json_encode ($ arr), 9). So I convert the array to a string with json_encode and then compress gzcompress. But I could not find much difference in the size of the given string. Before compression, the size is 488 KB, and after compression - 442 KB.

Is there any way to compress the string further?

Thanks in advance.

+6
source share
2 answers

How well the compression of your string will depend on the data you want to compress. If it consists mainly of random data, you will not achieve such significant improvements in size. There are many algorithms designed for specific use.

You should try to determine what your data for compression mainly consists of and then select the appropriate compression.

Now I can only refer to bzcompress , bzip usually has higher compression ratios than gzip.

+10
source

I'm not sure your numbers are correct, but you can use gzdeflate instead of gzcompress, since gzcompress adds 6 bytes to the output, 2 extra bytes at the beginning and 4 extra bytes at the end.

A simple test shows a 1756800 len string, compressed to 99 bits, by double compression, 5164 bits, if compressed once.

<?php $string = str_repeat('1234567890'.implode('', range('a', 'z')), 48800); echo strlen($string); //1756800 bytes $compressed = gzdeflate($string, 9); $compressed = gzdeflate($compressed, 9); echo '<br/>'.strlen($compressed).'<br/>';//99 bytes echo gzinflate(gzinflate($compressed)); ?> 
+10
source

Source: https://habr.com/ru/post/917846/


All Articles