Home >Backend Development >PHP Tutorial >How to Handle Malformed UTF-8 Characters during JSON Encoding in PHP?

How to Handle Malformed UTF-8 Characters during JSON Encoding in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-19 08:00:03842browse

How to Handle Malformed UTF-8 Characters during JSON Encoding in PHP?

Fixing Malformed UTF-8 Characters during PHP JSON Encoding

Introduction:

JSON is a popular data format for transmitting and storing data in a platform-independent manner. However, issues may arise when attempting to encode data containing non-UTF-8 characters. This discussion addresses a common problem encountered during JSON encoding in PHP, specifically related to malformed UTF-8 characters.

Problem:

When using json_encode($data) to encode an array containing a field with Russian characters, an error can occur due to malformed UTF-8 characters within the text. Utilizing mb_detect_encoding() reveals that the field is correctly encoded as UTF-8. Attempts to use utf8_encode on the data result in a bypass of the error, but it compromises the data's integrity.

Solution:

The issue lies in the presence of non-UTF-8 characters within the supposedly UTF-8 encoded text. To resolve this, remove any non-UTF-8 characters from the text, ensuring that the encoding remains consistent throughout.

The following code effectively addresses this problem:

<code class="php">$data['name'] = mb_convert_encoding($data['name'], 'UTF-8', 'UTF-8');</code>

This code converts the value of the 'name' key in the $data array to UTF-8 encoding, overwriting any non-UTF-8 characters with their UTF-8 equivalents. The result is a UTF-8-compliant string that can be successfully encoded as JSON.

The above is the detailed content of How to Handle Malformed UTF-8 Characters during JSON Encoding 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