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

The solution of garbled Code in opening json File by php

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

Share

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

This article mainly shows you the "php open json file garbled solution", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "php open json file garbled solution" this article.

Php open json file garbled solutions: 1, to ensure that the database, front and background PHP page coding consistent; 2, the configuration of the PHP function urlencode and JS function decodeURI (); 3, the use of string encoding conversion function.

Operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

Php open json file garbled how to do?

PHP JSON Chinese garbled code solution Daquan

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 "php Open json File garbled solution". 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

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report