Home Backend Development PHP Problem How to solve the Chinese garbled problem in php html page

How to solve the Chinese garbled problem in php html page

Jan 20, 2023 am 09:21 AM
php html Garbled characters

Solution to the Chinese garbled problem in php html page: 1. Set the character encoding of the entire page through "" on the front end; 2. In php, pass "header("content-type:text/html ; charset=xxx");" Just set the characters.

How to solve the Chinese garbled problem in php html page

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

How to solve the problem of Chinese garbled characters in php html page solve?

Front-end web pages, php and mysql database character encoding (solve Chinese and other garbled code problems

Web development often involves front-end web pages-php- —Data interaction between mysql usually does not cause any problems when the data is only in English, but once it involves Chinese, the character encoding in the three places is inconsistent (for example, the web page uses gbk and mysql uses utf-8) It may lead to garbled characters.

(Note: For information on character encoding, please refer to Baidu Encyclopedia: http://baike.baidu.com/view/1204863.htm?fr=aladdin)

Front-end web page coding:

Usually we all think that we can pass the item in the tag (such as ) to set the character encoding of the entire page. Most pages can use this method to tell the browser what encoding to use when displaying this page, but sometimes we will find that This sentence still doesn't work. No matter which xxx is, the browser always uses the same encoding.

This situation involves the header part of the http protocol communication. In fact, When a user browses a web page, the content sent by the server to the user not only includes our web page (including code content such as html/css/js), but also includes descriptive content called the header, which tells the client what is going to happen. The type of data received (whether it is HTML, plain text, multimedia files, etc.), size, source and other information (if you want to see this information, you can use the telnet tool (instead of through the browser) to initiate get and other requests yourself according to the http protocol Try). Since the header is sent before HTML, as part of HTML has a lower priority than the header. If the header already contains a description of the character encoding of the web page, the browser will eventually The web page will be parsed according to the character encoding set specified in the header.

In PHP, you can use header("content-type:text/html; charset=xxx"); to send headers about the character set Department.

As for the apache server, it has an AddDefaultCharset function, which means that the corresponding header will be set according to the server's default character set for each web page sent.

Check /etc/apache2/httpd.conf (before 2.4) or /etc/apache2/conf-available/charset.conf (2.4 and later). There is a sentence AddDefaultCharset xxx. If this sentence is not commented, then for each web page The function of adding a default character set header is turned on. At this time, setting the character set in the tag alone will have no effect.

Note: The encoding method indicated on the html page should be consistent with the encoding method used to actually save the html page (actually plain text).

Generally speaking, in order to be compatible with Chinese and even more other languages, using utf-8 encoding is the most trouble-free way, because utf-8 supports almost all commonly used languages ​​in the world.

mysql database encoding:

In the terminal mysql -uusername -ppassword and then enter the mysql control program, then type show variables like 'character%'; (note that The semicolon cannot be omitted when typing command statements or sql statements in the mysql terminal) You can see a picture similar to the following:

The above lists the character sets used by mysql at various levels. , where (*)

character-set-server/default-character-set: server character set, used by default.
character-set-database: Database character set.
character-set-table: Database table character set.
The priorities increase sequentially. Therefore, generally you only need to set the character-set-server, and do not specify a character set when creating databases and tables. In this way, the character-set-server character set is uniformly used.
character-set-client: The client’s character set. Client default character set. When a client sends a request to the server, the request is encoded in this character set.
character-set-results: Result character set. When the server returns results or information to the client, the results are encoded in this character set.
On the client side, if character-set-results is not defined, the character-set-client character set is used as the default character set. So only need to set the character-set-client character set.

So we will find that the character-set-server shown in the above picture does not use utf8 (note: in mysql, the utf-8 encoding method is expressed as utf8, without "-"). This is because the default storage method of mysql is latin1 without modification. In this case, when we use the mysql terminal to create databases and data tables, if the character set used is not specified in the sql statement, the encoding used for storage will be latin1. Obviously, Chinese characters are stored in This encoding method originally used to store Latin text will definitely be garbled when displayed.

So how to modify it? You can use set character-set-server = utf8; (because character-set-server has a high priority, just modifying it can achieve the effect of modifying the database storage encoding method). After that, when you use SQL to create a table in the terminal, the storage encoding method of the table is UTF-8.

However, this modification is only effective for the current service. Use quit; to exit and then enter the mysql terminal again and you will find that the character set has changed back to latin1. The information I have found so far shows that the method to make the modification permanent is only achieved by modifying the compilation parameters when recompiling mysql. If there are experts who know how to achieve this without compiling, please leave a message to let me know.

php encoding:

So, when it comes to mysql, how does php ensure that no garbled characters appear during data transmission when interacting with mysql?

According to the description at (*), in fact, in order to prevent garbled characters when storing and retrieving data from mysql, we only need to set the following three system parameters to match the server character set character-set -Server same character set. They are:
character_set_client: The client's character set.
character_set_results: Result character set.
character_set_connection: connection character set.
Setting these three system parameters can be achieved by sending the statement to MySQL: set names xxx (xxx can be utf8)

Therefore, when it comes to sending Chinese and other non-English characters from php to mysql, in After using the mysql_query("set names utf8"); statement after the mysql_connect statement (it is assumed that the database storage uses utf8), you can safely transmit and retrieve Chinese.

In addition, since HTML pages may actually be dynamically generated by PHP, how to ensure that the encoding method used by PHP dynamically generated pages is the same as that declared in the header or ?

Find the php.ini file in the php directory and modify default_charset = "utf-8" to make php use utf-8 to encode when outputting the page.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the Chinese garbled problem in php html page. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles