Take the output of gzdeflate()
, for example:
$a = gzdeflate('..........');
echo $a . "
" . strlen($a);
I get output like:
?Ӄ
5
So I've got a 5 byte string that contains characters which cannot be outputted properly, and hence cannot be copy and pasted.
Obviously, echo gzinflate('?Ӄ');
doesn't work, but echo gzinflate($a)
does.
Is there any way to get the actual contents of $a
onto my clipboard or output it in such a way that I could copy and paste it into gzinflate()
to retrieve the original string? The only workaround I've found is something like:
$a = base64_encode(gzdeflate('..........'));
echo $a;
Which gives me:
09ODAQA=
That's friendly enough to do echo gzinflate(base64_decode('09ODAQA='));
and get ..........
, but I'd like to skip the base64 functions if possible.
The problem is that you're channeling binary data through a text medium. If you require the data to be printed out on your screen where you will select and copy it, there's no way to transport binary data like that.
If this is happening on the command line, you could do it programatically without displaying the actual contents. Take OS X's pbcopy
and pbpaste
commands:
$ php test.php | pbcopy
$ pbpaste | someotherprogram
If you do require a visible textual representation, you need to ensure that the output is ASCII-safe (or at least "Unicode safe") and not raw binary data. For that you will need to base 64 or hex encode your binary data.
I guess your browser or console just eat some special chars like break line and other.
You can put this string to any file (file_put_contents()), and open this file throw notepad++ for example. and you will see these special chars in this file.