Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of garbled code submitted by josn in php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to solve the problem of garbled code submitted by josn in php, I believe most people don't know much about it, so share this article for your reference. I hope you will get a lot after reading this article. Let's learn about it together.

Php josn submit garbled solutions: 1, to ensure that the database and front and background PHP page coding consistent; 2, the configuration of the use of urlencode function; 3, the use of string encoding conversion function.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How to solve the problem of garbled code submitted by php josn?

We know that when using Ajax technology to interact with PHP background, Chinese garbled is a common thing. As a data exchange format similar to XML, JSON will also appear Chinese garbled when PHP is used for interaction. The way to solve PHP JSON Chinese garbled is actually similar to the solution of PHP Ajax Chinese character garbled. Below, I will introduce the solution to PHP JSON Chinese garbled in detail in the form of tutorials.

Why does Chinese garbled appear when PHP interacts with JSON?

Because JSON is the same as JS, the characters on the client side are processed in the form of UTF8, that is, the characters submitted or accepted by JSON are processed in the form of UTF8, when interacting with PHP, if the database coding and PHP page coding are not in UTF8 format, Chinese garbled will occur when PHP interacts with JSON.

The method of solving Chinese garbled codes in PHP JSON

The solution is actually much simpler after knowing the reasons for the Chinese garbled produced by the interaction between PHP and JSON.

The first method to solve PHP JSON Chinese garbled code is:

Ensure consistent coding of database, foreground and background PHP pages

The simplest way to solve the PHP JSON Chinese garbled code is to ensure that the database code and PHP page code are unified in UTF8 format. For PHP starter learners, it is best to use UTF8 format when setting up the database, which can reduce a lot of trouble.

Method 2 to solve the Chinese garbled code in PHP JSON:

Configure to use PHP function urlencode and JS function decodeURI ()

When PHP uses JSON to interact with foreground JS, it needs to use urlencode function to encode URL before using json_encode function, and read Chinese information after JS client uses JS function decodeURI () to decode URL.

Note: when interacting through JSON in a PHP page, such as converting an array to 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.

Method 3 to solve the Chinese garbled code in PHP JSON:

Use string encoding conversion function

When the database encoding and PHP page encoding are inconsistent due to various reasons, such as the existing database encoding uses GB2312, when using JSON interaction, it is necessary to use the string encoding conversion function to convert between character sets. The commonly used function is iconv, because iconv needs PHP configuration environment support, if not, you can write gbk and UTF8 transcoding functions to achieve transcoding.

At this point, the PHP JSON Chinese character garbled three solutions are introduced, in fact, in PHP development, encountered similar PHP and AJAX/JS interaction, generate Chinese garbled basically use these methods, the idea is similar.

The above is all the contents of this article entitled "how to solve the problem of josn submitting garbled codes in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development