Home >Backend Development >PHP Tutorial >How to Handle Malformed UTF-8 Characters in PHP JSON Encoding?
Handling Malformed UTF-8 Characters in PHP JSON Encoding
When using json_encode() to serialize an array containing Russian characters, you may encounter an error related to malformed UTF-8 characters. To resolve this issue, perform the following steps:
Step 1: Identify the Character Encoding
Use mb_detect_encoding() to determine the encoding of the field containing Russian characters. Confirm that it is indeed UTF-8, as reported by your example.
Step 2: Remove Non-UTF-8 Characters
While the field may be primarily encoded in UTF-8, it could still contain non-UTF-8 characters. To remove these, use the mb_convert_encoding() function:
<code class="php">$data['name'] = mb_convert_encoding($data['name'], 'UTF-8', 'UTF-8');</code>
This action will replace any non-UTF-8 characters with their equivalent placeholder representations, ensuring that the string remains valid for JSON encoding.
Step 3: Serialize with JSON Encoding
Once the non-UTF-8 characters are removed, you can successfully serialize the data using json_encode(). The resulting JSON will be valid and contain the correct representation of the Russian characters.
The above is the detailed content of How to Handle Malformed UTF-8 Characters in PHP JSON Encoding?. For more information, please follow other related articles on the PHP Chinese website!