Chinese characters in php web page are garbled

王林
Release: 2023-02-24 12:54:02
Original
7608 people have browsed it

Chinese characters in php web page are garbled

Configuring phpstudy, Chinese garbled characters appear when accessing the page. The following is the solution.

1. Encoding of PHP web page

1. The encoding of the PHP file itself and the encoding of the web page should match

a . If you want to use gb2312 encoding, then php should output the header: header("Content-Type: text/html; charset=gb2312"), and add <meta http-equiv=" to the static page Content-Type" content="text/html; charset=gb2312">, the encoding format of all files is ANSI, which can be opened with Notepad, save as and select the encoding as ANSI, overwriting the source file .

b. If you want to use utf-8 encoding, then php should output the header: header("Content-Type: text/html; charset=utf-8"), add it to the static page <meta http-equiv="Content-Type" content="text/html; charset=utf-8">, the encoding format of all files is utf-8.

Saving as utf-8 may be a bit troublesome. Generally, utf-8 files will have BOM at the beginning. If you use session, there will be problems. You can save it with editplus. In editplus, go to Tools->Parameter Selection- >File->UTF-8 signature, select Always delete, then save to remove the BOM information.

2. PHP itself is not Unicode

All functions such as substr must be changed to mb_substr (mbstring extension needs to be installed), or iconv transcoding must be used.

2. Data interaction between PHP and Mysql

1. The coding of PHP and database should be consistent

Modify mysql configuration The file my.ini or my.cnf, mysql is best encoded with utf8.

Chinese characters in php web page are garbled

#2. Add mysql_query("set names 'encoding'")# before the PHP program that needs to perform database operations.

##The encoding is consistent with the php encoding. If the php encoding is gb2312, then the mysql encoding is gb2312. If it is utf-8, then the mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data.

3. PHP is related to the operating system

The encoding of Windows and Linux is different. In the Windows environment, when calling a PHP function, if the parameter is utf- 8 Encoding errors will occur, such as

move_uploaded_file(), filesize(), readfile(), etc. These functions are often used when processing uploads and downloads. , an error may occur when calling.

Although these errors will not occur when using gb2312 encoding in a Linux environment, the saved file name will be garbled and the file cannot be read. In this case, you can first convert the parameters into the encoding recognized by the operating system and perform encoding conversion. You can use

mb_convert_encoding(string, new encoding, original encoding) or iconv(original encoding, new encoding, string), so that the file name saved after processing will not be garbled. You can also read files normally and upload and download files with Chinese names.

In fact, there is a better solution, which is to completely separate from the system, and there is no need to consider the encoding of the system. You can generate a sequence of only letters and numbers as the file name, and save the original name with Chinese characters in the database. In this way, there will be no problem when calling

move_uploaded_file(). When downloading, you only need to move the file The name was changed to the original Chinese name. The code to implement the download is as follows:

Chinese characters in php web page are garbled

In the end, you can actually modify a php page and add an output header.

Chinese characters in php web page are garbled

Recommended tutorial:

PHP video tutorial

The above is the detailed content of Chinese characters in php web page are garbled. 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