How to Decode and Encode JSON with Unicode Characters in PHP?

Linda Hamilton
Release: 2024-10-31 13:18:30
Original
432 people have browsed it

How to Decode and Encode JSON with Unicode Characters in PHP?

PHP Decoding and Encoding JSON with Unicode Characters

Unicode Character Decoding Issues

When attempting to decode JSON containing unicode characters, you may encounter issues if the characters fall within certain bounds. Specifically, characters such as "" and "" prohibited from appearing in JSON strings. Additionally, control characters are not allowed.

UTF-8 Encoding and Decoding

To address this, you can use utf8_encode to allow the string to be decoded with json_decode. However, this may result in the characters being mangled. For instance, "Odómetro" would be converted to "Odómetro".

Re-Encoding and Character Escaping

Upon re-encoding the array, the character is escaped to ASCII, which is correct according to the JSON spec: "Tag"=>"Odu00f3metro". To unescape the character, you could utilize the JSON_UNESCAPED_UNICODE option for `json_encode. However, this is only available in PHP 5.4 or later.

Alternative Solution Using Regex

If you're limited to PHP 5.3, you can employ a regex-based solution:

$json = json_encode($array, JSON_UNESCAPED_SLASHES);

// Replace escaped unicode characters with their UTF-8 equivalents
$json = preg_replace('/\\u([0-9a-fA-F]{4})/', '&#x;', $json);
Copy after login

By using the JSON_UNESCAPED_SLASHES flag, you prevent the slashes () from being escaped in the JSON string. The regex pattern then matches escaped unicode characters (u####) and replaces them with their UTF-8 equivalents.

The above is the detailed content of How to Decode and Encode JSON with Unicode Characters in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!