UTF-8 to Hexadecimal Entities Conversion in PHP's json_encode: Delving into the Behavior and Solutions
In PHP applications that handle multilingual data, the json_encode function often poses a perplexing challenge. When Unicode characters are encountered, they are automatically converted into hexadecimal entities, disrupting the desired Unicode output.
Explanation:
This behavior stems from the default encoding mechanism of json_encode. It utilizes JSON, which expects Unicode characters to be represented as hexadecimal entities to ensure compatibility across different systems and browsers. However, this conversion can be undesirable when dealing with specific scenarios.
Solution:
To counter this behavior, a solution lies within a newly introduced option in PHP 5.4.0, JSON_UNESCAPED_UNICODE. This option instructs json_encode to leave Unicode characters intact without converting them into hexadecimal entities. By utilizing this option, the expected UTF-8 characters can be retained:
json_encode($text, JSON_UNESCAPED_UNICODE);
This approach allows the following input and output:
INPUT echo $text; OUTPUT База данни грешка.
INPUT json_encode($text, JSON_UNESCAPED_UNICODE); OUTPUT База данни грешка.
With this technique, the complexities of Unicode conversion in PHP's json_encode function can be effectively resolved, ensuring the preservation of desired Unicode output.
The above is the detailed content of Why Does `json_encode` Convert UTF-8 to Hexadecimal Entities in PHP?. For more information, please follow other related articles on the PHP Chinese website!