PHP commonly used string compression and decompression, php compression and decompression of strings

The string compression methods provided by php are:

1. gzcompress - compress a string

This function compresses the given string data format using ZLIB .

2. gzencode - creates a string gzipped

This function returns a compressed version of the input data compatible with the output of the program gzip

3. gzdeflate - shrink a string

This function compresses the given string using the reduced data format.

4.bzcompress — compress a string into bzip2 encoded data

bzcompress() compresses the specified string and returns the data in bzip2 encoding.

gzcompress is the fastest and has a higher compression ratio.

gzdeflate has the highest compression ratio, slightly slower than gzcompress

gzencode is close to gzdeflate, gzdeflate has a slight advantage

bzcompress is the slowest and has the slowest compression ratio.

details as follows:

The following php code compresses and decompresses strings through gzcompress and gzuncompress, and the compression level can be set

$str = ‘Hello I am a very very very very long string';

$compressed = gzcompress($str, 9);//The compression level is 9

$uncompressed = gzuncompress($compressed);

echo $str, "\n";

echo $uncompressed, “\n”;

echo base64_encode($compressed), “\n”;

echo bin2hex($compressed), “\n”;

echo urlencode($compressed), “\n”;

Related: PHP commonly used string compression and decompression, php compression and decompression of strings