Is there a way I can compress a long string (e.g. a long JSON string) in PHP and then decompress it in JavaScript?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 😉