search
HomeBackend DevelopmentPHP TutorialSummary of common solutions to Chinese garbled characters in PHP, _PHP tutorial

Summary of common solutions to Chinese garbled characters in PHP, _PHP tutorial

Jul 13, 2016 am 09:44 AM
phpChineseGarbled characterscommoncommon problemdevelopSummarizemethodyesofsolve

A summary of common solutions to Chinese garbled characters in PHP,

PHP Chinese garbled characters are one of the common problems in PHP development. PHP Chinese garbled characters sometimes occur in the web page itself, some occur in the process of MySQL interaction, and sometimes are related to the operating system. Here is a summary.

1. The first is the encoding of the 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 , the encoding format of all files is ANSI, you can open it with Notepad, save as, select the encoding as ANSI, and overwrite 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"), and add , 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 use editplus to save. 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 use iconv to transcode.

2. Data interaction between PHP and Mysql

The encoding of PHP and database should be consistent

1. Modify the mysql configuration file my.ini or my.cnf. It is best to use utf8 encoding for mysql

[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-storage-engine=MyISAM
Under [mysqld] Add:
default-collation=utf8_bin
init_connect='SET NAMES utf8'

2. Add mysql_query("set names" before the PHP program that needs to perform database operations 'Encoding'");, 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 Windows environment, when calling PHP functions, if the parameters are utf-8 encoded, errors will occur, such as move_uploaded_file(), filesize(), readfile(), etc. These functions are often used when processing uploads and downloads. The following may occur when calling. Error below:

Warning: move_uploaded_file()[function.move-uploaded-file]: failed to open stream: Invalid argument in ...

Warning: move_uploaded_file()[function.move-uploaded-file]:Unable to move '' to '' in ...

Warning: filesize() [function.filesize]: stat failed for ... in ...

Warning: readfile() [function.readfile]: failed to open stream: Invalid argument in ..

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 an encoding recognized by the operating system. code, encoding conversion 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 is There will be no garbled characters, files can be read normally, and files with Chinese names can be uploaded and downloaded.

In fact, there is a better solution, completely disconnected 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 In the database, there will be no problem when calling move_uploaded_file(). When downloading, you only need to change the file name to the original name with Chinese characters. The code to implement downloading is as follows

header("Pragma: public");

header("Expires: 0");

header("Cache-Component: must-revalidate, post-check=0, pre-check=0");

header("Content-type: $file_type");

header("Content-Length: $file_size");

header("Content-Disposition: attachment; filename="$file_name"");

header("Content-Transfer-Encoding: binary");

readfile($file_path);

$file_type is the type of file, $file_name is the original name, and $file_path is the address of the file saved on the service.

4. Let’s summarize why the codes are garbled

Generally speaking, there are two reasons for the appearance of garbled characters. The first is due to encoding (charset) The settings are incorrect, causing the browser to parse with the wrong encoding, resulting in a messy "heavenly book" filling the screen. Secondly, the file is opened with the wrong encoding and then saved. For example, a text file was originally It is encoded in GB2312, but it is opened and saved in UTF-8 encoding. To solve the above garbled code problem, you first need to know which aspects of development involve coding:

1. File encoding: refers to the encoding in which the page file (.html, .php, etc.) itself is saved. Notepad and Dreamweaver The file encoding will be automatically recognized when the page is opened, so there will be no problems. However, ZendStudio does not automatically recognize the encoding. It will only open the file in a certain encoding based on the configuration of the preferences. I accidentally opened the file with the wrong encoding while working, and after making the modifications, as soon as I saved it, garbled characters appeared (I know this very well).

2. Page declaration encoding: In the HTML code HEAD, you can use To tell the browser what encoding is used for the web page. Currently, XXX mainly uses GB2312 and UTF-8 in Chinese website development.

3. Database connection encoding: refers to the encoding used to transmit data to the database when performing database operations. It should be noted here that it should not be confused with the encoding of the database itself, such as the internal default of MySQL It 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 characters: the above three coding settings are inconsistent. Since most of the various codings are ASCII compatible, English symbols will not appear. Chinese That's bad luck.

5. Fight some common error situations and solutions:

1. The database uses UTF8 encoding, and the page declaration encoding is GB2312 , which is the most common cause of garbled characters. At this time, the direct SELECT data in the PHP script will be garbled, and you need to use it before querying: mysql_query("SET NAMES GBK"); to set the MYSQL connection encoding and ensure that the page declaration encoding is consistent with the connection encoding set here (GBK is an extension of GB2312 ). If the page is UTF-8 encoded, you can use: mysql_query("SET NAMES UTF8");
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.

Note: In fact, 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 in [client] default-character-set and default-character-set in [mysqld] To set the encoding used by default for client connections and internal databases respectively. The encoding we specified above is actually the command line parameter when the MYSQL client connects to the server. character_set_client, to tell the MYSQL server what encoding the client data received is, instead of using the default encoding.

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 characters. More often than not, it is modified after publishing. Some minor bugs are caused by 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. code.

3. Some friends who rent virtual hosts still have garbled codes even though the above three encodings are set correctly. For example, the web page is GB2312 Encoded, IE and other browsers always recognize it as UTF-8 when opened. The page HEAD has stated that it is GB2312. Manually modify the browser encoding to GB2312. The subsequent page displays normally. The reason is that the server Apache sets the global default encoding of the server and adds AddDefaultCharset in httpd.conf. UTF-8 . 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 this to the configuration file of their own virtual machine. AddDefaultCharset GB2312 to override the global configuration, or configure it in .htaccess in your own directory.

Summary: In a word, the best and fastest way to solve the Chinese garbled code in PHP is to make the coding declared by the page consistent with the internal coding of the database. If the page number applied for the page is inconsistent with the internal coding of the database , just set the connection encoding, mysql_query("SET NAMES XXX "); XXX is the connection encoding. This will definitely solve the problem of garbled characters.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1048771.htmlTechArticleSummary of common solutions to Chinese garbled characters in PHP. PHP Chinese garbled characters are one of the common problems in PHP development. PHP Chinese garbled characters sometimes occur in the web page itself, and some occur in the MySQL interaction process...
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's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.