Answer the question
In order to leave comments, you need to log in
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
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""
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.
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 questionAsk a Question
731 491 924 answers to any question