Y
Y
yetanotherape2010-09-27 07:57:05
PHP
yetanotherape, 2010-09-27 07:57:05

json_encode in PHP: Data Growth

PHP has built-in facilities for converting data to json. Consider an example:

echo json_encode(iconv('cp1251', 'utf-8', 'тест'));

It will return "\ u0442\ u0435\ u0441\ u0442" (spaces so as not to tempt the parser) - this is 26 bytes, not 10 at all, as we would like.

How so?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kirill Dlussky, 2012-05-07
@Dlussky

Emmm. What's the problem gentlemen? Confident in the encoding - go ahead)
php -r 'var_dump(json_encode("test"));'
string(26) ""\u0442\u0435\u0441\u0442""
php -r 'var_dump(json_encode("test", JSON_UNESCAPED_UNICODE));'
string(10) ""test""

E
Eugene, 2010-09-27
@Nc_Soft

It was already in q& about it. What to do? Accept and enable gzip.

A
Anton Korzunov, 2010-09-27
@kashey

Use self-made json:: encode - there is code for 20 lines.
And even feed him cp1251 without an envelope.
Such solutions have only one drawback - they work hundreds of times as built-in solutions.

M
MT, 2010-09-27
@MTonly

Such encoding of non-Latin characters is, so to speak, the only reliable way to guarantee the exact representation of such characters, regardless of the encoding used on the web page. But it's a pity, of course, that there is json_encode()no parameter that allows you to disable such encoding if the encoding of the page and JSON data are certainly the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question