Is there a way I can compress a long string (e.g. a long JSON string) in PHP and then decompress it in JavaScript?
Asked
Active
Viewed 1,258 times
5
-
1http://stackoverflow.com/questions/2037349/how-to-compress-json-with-php – Haim Evgi Mar 16 '11 at 06:13
3 Answers
3
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 ;-)

Pascal MARTIN
- 395,085
- 80
- 655
- 663
-4
<?php
$str1 = "Test";
$str2 = "Test";
if ($str1 == "Test") echo "OK-1";
if ($str1 == $str2) echo "OK-2";
?>

SHRUTI PATEL
- 125
- 4
-
1Your "answers" have several shortcomings; they are not self-explanatory, they don't address OP's question, and there are more than one. – jensgram Mar 16 '11 at 09:38
-
1compress, not compare. And spamming the question with multiple useless answers is frowned upon – Stephen Perelson Mar 17 '11 at 12:14
-4
function isSameString( s1, s2 )
{
alert( "s1: " + s1.toString() );
alert( "s2: " + s2.toString() );
return s1.toString() == s2.toString();
}

Thor
- 45,082
- 11
- 119
- 130

SHRUTI PATEL
- 125
- 4
-
2
-
2And why would anyone do this when `return s1.toString() == s2.toString()` is more readable *and* does the exact same? – jensgram Mar 16 '11 at 09:36
-
if ( s1.toString() == s2.toString() ) this i do because i use function........... if we want to apply that same for more time so............... otherwise at all place we have to write – SHRUTI PATEL Mar 17 '11 at 08:36
-
1What? What I was saying is that `return s1.toString() == s2.toString()` is *identical* to `if (s1.toString() == s2.toString()) { return true; } else { return false; }`. – jensgram Mar 17 '11 at 08:38