N
N
nfedyk2013-09-23 19:34:09
PHP
nfedyk, 2013-09-23 19:34:09

json_encode in PHP turns Cyrillic into \u041D codes

Actually a subject. How to fix this behavior?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nazar Mokrinsky, 2013-09-23
@nazarpc

json_encode($str, JSON_UNESCAPED_UNICODE);

M
MT, 2013-09-25
@MTonly

Actually, it is not necessary to “fix” (although in terms of perfectionism, I understand you well). These are legitimate JS strings that are displayed correctly in the browser. The amount of data, however, is larger. But, on the other hand, Gzip compression minimizes this factor. ;-)
You can also do this:

$json = defined('JSON_UNESCAPED_UNICODE')
      ? json_encode($data, JSON_UNESCAPED_UNICODE)
      : json_encode($data);

Then, in recent versions of PHP, the JSON code will be the most optimal in terms of volume, and in older versions it will be only slightly less optimal.

D
dwl is, 2013-11-21
@dewil

For PHP 5.3 and below, you can do this

preg_replace_callback('/\\\u([01-9a-fA-F]{4})/', 'prepareUTF8', json_encode($array))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question