The solution would be to use gzip compression of the entire output of your PHP script - and let the browser handle client-side decompression.
If you are working with Apache, you can use mod_deflate .
Else, in PHP you can use ob_gzhandler - see the example on this manual page.
Then, on the client side (browser), you have nothing to do: decompressions will be processed automatically.
Like sidenote: using mod_deflate , you can configure CSS, Javascript, HTML (well, all data that is text) to be compressed the same way - which will reduce the size of your pages and speed up their loading; therefore, it is worth a little investigation :-)
source share