search
HomeBackend DevelopmentPHP ProblemHow to solve the json php Chinese garbled problem

Solution to json php Chinese garbled code: 1. Ensure that the database and front-end and back-end PHP page encodings are consistent; 2. Configure and use the PHP function urlencode and JS function decodeURI; 3. Use the string encoding conversion function "iconv".

How to solve the json php Chinese garbled problem

#Environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

Recommended: "PHP Video Tutorial"

PHP JSON Chinese garbled solution solution

We know that we are using Ajax technology When interacting with the PHP backend, Chinese garbled characters are common. As JSON is a data exchange format similar to XML, Chinese garbled characters will also appear when PHP is used to interact. The method and idea of ​​​​solving Chinese garbled characters in PHP JSON is actually the same as PHP The solution to garbled Chinese characters in Ajax value transfer is similar. Below, I will introduce in detail the method to solve the problem of Chinese garbled characters in PHP JSON in the form of a tutorial.

Why do Chinese garbled characters appear when PHP interacts with JSON?

Since JSON is the same as JS, the characters on the client are processed in the form of UTF8, that is, the characters returned by JSON submission or acceptance are processed in the form of UTF8. When interacting with PHP, if the database encoding and PHP page encoding When UTF8 format is not used, Chinese characters will be garbled when PHP interacts with JSON.

Methods to solve Chinese garbled characters in PHP JSON

After knowing the reasons why Chinese garbled characters are generated when PHP interacts with JSON, the solution is actually much simpler.

Method 1 to solve the Chinese garbled PHP JSON: Ensure that the encoding of the database, front and back PHP pages is consistent

The simplest way to solve the Chinese garbled PHP JSON is to ensure the database encoding , PHP page encoding uniformly uses UTF8 format. For beginners of PHP, it is best to use UTF8 format for the database when building a website, which can reduce a lot of trouble.

Solution to PHP JSON Chinese garbled method two: Configure and use the PHP function urlencode and JS function decodeURI()

When PHP uses JSON to interact with the front-end JS, use json_encode Before the function, you need to use the urlencode function to encode the URL. The JS client uses the JS function decodeURI() to decode the URL before reading the Chinese information.

Note: When interacting through JSON in a PHP page, such as converting an array into JSON format, first use the urlencode function to URL-encode the array key and value, and then use the json_encode function and urldecode function.

Three methods to solve PHP JSON Chinese garbled code: Use string encoding conversion function

When the database encoding and PHP page encoding are inconsistent due to various reasons, such as the existing The database encoding uses GB2312. When interacting with JSON, you need to use the string encoding conversion function to convert between character sets. Commonly used functions include iconv. Since iconv requires PHP configuration environment support, if it is not supported, you can implement encoding conversion by writing your own gbk and UTF8 encoding conversion functions.

At this point, the three solutions to garbled Chinese characters in PHP JSON have been introduced. In fact, in PHP development, when encountering similar interactions between PHP and AJAX/JS, these methods are basically used when Chinese garbled characters are generated. The idea is similar.

The above is the detailed content of How to solve the json php Chinese garbled problem. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version