Any way to return PHP `json_encode` with encode UTF-8 and not Unicode? [duplicate] Any way to return PHP `json_encode` with encode UTF-8 and not Unicode? [duplicate] json json

Any way to return PHP `json_encode` with encode UTF-8 and not Unicode? [duplicate]


{"a":"\u00e1"} and {"a":"รก"} are different ways to write the same JSON document; The JSON decoder will decode the unicode escape.

In php 5.4+, php's json_encode does have the JSON_UNESCAPED_UNICODE option for plain output. On older php versions, you can roll out your own JSON encoder that does not encode non-ASCII characters, or use Pear's JSON encoder and remove line 349 to 433.


I resolved my problem doing this:

  • The .php file is encoded to ANSI. In this file is the function to create the .json file.
  • I use json_encode($array, JSON_UNESCAPED_UNICODE) to encode the data;

The result is a .json file encoded to ANSI as UTF-8.


This function found here, works fine for me

function jsonRemoveUnicodeSequences($struct) {   return preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($struct));}