Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String compression in PHP and JavaScript

Is there a way I can compress a long string (e.g. a long JSON string) in PHP and then decompress it in JavaScript?

like image 293
Rendicahya Avatar asked Nov 27 '22 20:11

Rendicahya


1 Answers

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 ;-)

like image 130
Pascal MARTIN Avatar answered Dec 15 '22 00:12

Pascal MARTIN