What to do if dw php web page has Chinese garbled characters?

藏色散人
Release: 2023-03-17 15:20:02
Original
6741 people have browsed it

dw Solution to garbled Chinese characters in php web pages: 1. Set the title encoding to UTF8; 2. When using Dreamweaver to create a new php file, declare the format utf through "" -8; 3. Set the MYSQL connection encoding through "mysql_query("SET NAMES GBK");". ="utf-8">

What to do if dw php web page has Chinese garbled characters?

The operating environment of this tutorial: Windows 7 system, Dreamweaver version 8, Dell G3 computer.

What should I do if the dw php webpage has Chinese garbled characters?

How to solve the problem of Chinese garbled characters in the preview interface of Dreamweaver when running the php file

Doing website development requires dealing with different problems in many countries As for language, we often encounter a mix of Chinese and English. When using Dreamweaver to create and run PHP code, the Chinese preview window sometimes displays garbled characters, but English can be displayed normally.

What to do if dw php web page has Chinese garbled characters?
What to do if dw php web page has Chinese garbled characters?
#This happens The main reasons are:

1. File encoding: refers to the encoding in which the page file (.html, .php, etc.) itself is saved. Notepad and Dreamweaver will automatically recognize the file encoding when opening the page, so there will be less problems. However, ZendStudio does not automatically recognize the encoding. It will only open the file with a certain encoding. If you accidentally open the file with the wrong encoding while working, and save it after making the modification, garbled characters will appear.

Solution: Page settings, title/encoding, set to UTF8

What to do if dw php web page has Chinese garbled characters?

2, page Declaration encoding:

HTML page encoding settingsIn the HTML code HEAD, it can be used to tell the browser what encoding the web page uses. Currently, XXX mainly uses GB2312 and UTF in Chinese website development -8 Two encodings. When using Dreamweaver to create a new php file, you need to declare the format utf-8:



What to do if dw php web page has Chinese garbled characters?
3. Database connection encoding: refers to which encoding is used to transmit data to the database when performing database operations. What needs to be noted here is not to contact the database itself. Coding confusion, for example, MySQL's internal default is latin1 encoding, which means that Mysql stores data in latin1 encoding, and data transmitted to Mysql in other encodings will be converted into latin1 encoding.

Knowing where coding is involved in WEB development, you also know the cause of garbled codes: the above three coding settings are inconsistent. Since most of the various codings are compatible with ASCII, English symbols will not Garbled characters appear, and Chinese characters appear garbled.

Common errors and solutions:

1. The database uses UTF8 encoding, and the page declaration encoding is GB2312. This is the most common cause of garbled characters.

At this time, the directly SELECT data in the PHP script will be garbled. You need to use it before querying: mysql_query("SET NAMES GBK"); to set the MYSQL connection encoding to ensure that the page declaration encoding is consistent with the setting here. The specified connection encoding is consistent (GBK is an extension of GB2312). If the page is UTF-8 encoded, you can use: mysql_query("SET NAMES UTF8");
PHP encoding setting
Write the first sentence in the php code:

<?php
header("Content-type:text/html;charset=utf-8");
?>
Copy after login

Modify the php.ini file

Modify "/etc/php5/apache2/php.ini" and change

default_charset = "iso-8859-1"
Copy after login

to default_charset = "utf-8"

Mysql encoding settings

<?php
mysql_query(&#39;SET NAMES UTF8&#39;);
?>
Copy after login

Note that it is UTF8 instead of the commonly used UTF-8. If the encoding of the page declaration is consistent with the internal encoding of the database, you do not need to set the connection encoding.

The data input and output of MYSQL is more complicated than what is mentioned above. There are 2 default encodings defined in the MYSQL configuration file my.ini, which are default-character-set in [client] and [mysqld]. The default-character-set is used to set the encoding used by the client connection and the database internally by default. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server, which tells the MYSQL server what encoding the client data received is, instead of using the default encoding.

Based on the previous ones, you also need to add database coding before querying/modifying/adding your data.

2. The page declaration encoding is inconsistent with the encoding of the file itself. This rarely happens because if the encoding is inconsistent, what the artist sees in the browser when creating the page will be garbled. More often than not, it is caused by fixing some minor bugs after release, opening the page in the wrong encoding and then saving it. Or you use some FTP software to directly modify files online, such as CuteFTP. Due to incorrect software encoding configuration, the wrong encoding is converted.

3. When renting a virtual host, if the above three encodings are set correctly, sometimes there are still garbled characters. For example, if the web page is encoded in GB2312, it is always recognized as UTF-8 when opened by browsers such as IE. The HEAD of the web page has already stated that it is GB2312. After manually changing the browser encoding to GB2312, the page displays normally. The reason is that the server Apache sets the global default encoding of the server and adds AddDefaultCharset UTF-8 in httpd.conf. At this time, the server will first send the HTTP header to the browser, and its priority is higher than the encoding declared in the page. Naturally, the browser will recognize it incorrectly. There are two solutions. Administrators should add AddDefaultCharset GB2312 to the configuration file of their own virtual machine to override the global configuration, or configure it in .htaccess in their own directory.

Summary: To solve the Chinese garbled code in PHP, the fastest solution is to make the encoding declared on the page consistent with the internal encoding of the database. If the page number applied for the page is inconsistent with the internal encoding of the database, set the connection encoding, mysql_query ("SET NAMES XXX "); XXX is the connection code. Such as mysql_query('SET NAMES UTF8'); [2]

Finally, Chinese is successfully displayed:

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>中文</title>
</head>

<body>

<h1>中文</h1>

<?php
echo "Hello World!";
?>
Copy after login

What to do if dw php web page has Chinese garbled characters?

Recommended study: "PHP Video Tutorial"

The above is the detailed content of What to do if dw php web page has Chinese garbled characters?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template