Is there a way I can compress a long string (e.g. a long JSON string) in PHP and then decompress it in JavaScript?
A solution would be to use gzip compression of the whole output of your PHP script -- and to let the browser handle the decompression, on the client-side.
If you are working with Apache, you can use mod_deflate
.
Else, in PHP, you can use ob_gzhandler
-- see the example on that manual page.
Then, on the client-side (browser), you have nothing to do : the decompression will be dealt with automatically.
As a sidenote : using mod_deflate
, you can configure so CSS, Javascript, HTML (well, all data that are text) are compressed the same way -- which will reduce the size of your pages, and speed up their loading a bit ; so, it's worth investigating a bit ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With