Home >Backend Development >PHP Tutorial >Here are a few question-based titles that fit your article: * Why is json_encode Still Escaping Non-ASCII Characters in my PHP Output? * How Can I Ensure UTF-8 Encoding in PHP\'s json_encode Output f
Ensuring UTF-8 Encoding in PHP's json_encode Output for Non-ASCII Characters
Despite efforts to enable UTF-8 encoding through mb_internal_encoding and utf8_encode, json_encode continues to return Unicode-escaped characters in the resulting JSON string. This issue can be addressed through various approaches.
JSON_UNESCAPED_UNICODE Option in PHP 5.4
If using PHP 5.4 or later, leverage the JSON_UNESCAPED_UNICODE option when invoking json_encode. This option explicitly disables Unicode escaping, leading to the expected output: {"a":"á"}.
Custom JSON Encoder with Non-ASCII Support
For earlier PHP versions, create a custom JSON encoder that does not escape non-ASCII characters. This approach ensures that the JSON string accurately represents the original input.
Pear's JSON Encoder Modification
An alternative solution involves modifying Pear's JSON encoder by removing lines 349 to 433. This change effectively disables Unicode escaping within the encoder, resulting in the desired output.
Conclusion
These techniques provide viable ways to output UTF-8 encoded JSON strings, enabling accurate representation of non-ASCII characters without Unicode escaping.
The above is the detailed content of Here are a few question-based titles that fit your article: * Why is json_encode Still Escaping Non-ASCII Characters in my PHP Output? * How Can I Ensure UTF-8 Encoding in PHP\'s json_encode Output f. For more information, please follow other related articles on the PHP Chinese website!