Home  >  Article  >  Backend Development  >  How to Encode and Decode JSON with Unicode Characters in PHP?

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

Barbara Streisand
Barbara StreisandOriginal
2024-10-31 05:01:30986browse

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

Encoding and Decoding JSON with Unicode Characters in PHP

In PHP, dealing with JSON strings containing unicode characters can present challenges. This article addresses the issues commonly encountered when decoding and encoding such strings.

Decoding Unicode Characters

To decode JSON with unicode characters, you may encounter issues if the characters are not properly encoded. The JSON specification allows any unicode character except for double quotes, backslashes, and control characters. However, certain unicode characters may not decode correctly in Python or other programming languages.

Encoding Unicode Characters

When encoding JSON with unicode characters, the result may be encoded with escaped unicode sequences, which is valid according to the JSON specification. However, you may desire to preserve the original unicode characters in their unescaped form.

Using unescaped_unicode Option

PHP 5.4 introduced the JSON_UNESCAPED_UNICODE option for json_encode(), which provides the ability to encode unicode characters without escaping them. However, if you are using PHP 5.3, this option is not available.

Alternative Solution for PHP 5.3

For PHP 5.3, you can use a regular expression-based approach to unescape the encoded unicode sequences. One way to do this is:

<code class="php">$pattern = '/"\\u([0-9a-fA-F]{4})"/';
$replaced = preg_replace($pattern, '"\u"', $encodedJson);</code>

This regular expression searches for escaped unicode sequences in the form of "uXXXX" and replaces them with the unescaped unicode character.

Example Code

To encode a JSON string with unicode characters and unescape them during decoding, you can use the following code:

<code class="php">$data = ['Tag' => 'Odómetro'];
$encodedJson = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$decodedData = json_decode($encodedJson, true);

print_r($decodedData); // Output: [Tag] => Odómetro</code>

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

Statement:
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