" on the front end; 2. In php, you can set the characters through "header("content-type:text/html; charset=xxx");"."/> " on the front end; 2. In php, you can set the characters through "header("content-type:text/html; charset=xxx");".">
search
HomeBackend DevelopmentPHP ProblemHow to solve the Chinese garbled problem in php html page
How to solve the Chinese garbled problem in php html pageJan 20, 2023 am 09:21 AM
phphtmlGarbled 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
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment