Home Backend Development PHP Tutorial Analysis of the causes and solutions of Chinese garbled characters in PHP_PHP Tutorial

Analysis of the causes and solutions of Chinese garbled characters in PHP_PHP Tutorial

Jul 15, 2016 pm 01:30 PM
php one Chinese Garbled characters Appear analyze Method reason document of coding Web page solve

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 must output the header: header( "Content-Type: text/html; charset=gb2312"), add to the static page, and the encoding format of all files is ANSI, you can open it with Notepad, save as and 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 the database should be consistent

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

<ol class="dp-xml">
<li class="alt"><span><span>[mysql]  </span></span></li>
<li>
<span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">utf8</span><span> </span>
</li>
<li class="alt"><span>[mysqld]  </span></li>
<li>
<span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">utf8</span><span> </span>
</li>
<li class="alt">
<span class="attribute">default-storage-engine</span><span>=</span><span class="attribute-value">MyISAM</span><span> </span>
</li>
<li><span>在[mysqld]下加入:  </span></li>
<li class="alt">
<span class="attribute">default-collation</span><span>=</span><span class="attribute-value">utf8_bin</span><span> </span>
</li>
<li>
<span class="attribute">init_connect</span><span>=</span><span class="attribute-value">'SET NAMES utf8'</span><span> </span>
</li>
</ol>
Copy after login

2. Add mysql_query("set names 'coding'"); before the PHP program that needs to perform database operations. The encoding is consistent with the PHP encoding. If If the php encoding is gb2312, then the mysql encoding is gb2312. If it is utf-8, then the mysql encoding is utf8. In this way, PHP Chinese garbled characters will not appear 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 PHP functions, if the parameters are utf-8 encoding, errors will occur, such as move_uploaded_file(), filesize(), readfile(), etc. These functions are often used when processing uploads and downloads. The following errors may occur when calling:

<ol class="dp-xml">
<li class="alt"><span><span>Warning: move_uploaded_file()[function.move-uploaded-file]:failed to open stream: Invalid argument in ...  </span></span></li>
<li><span> </span></li>
<li class="alt"><span>Warning: move_uploaded_file()[function.move-uploaded-file]:Unable to move '' to '' in ...  </span></li>
<li><span> </span></li>
<li class="alt"><span>Warning: filesize() [function.filesize]: stat failed for ... in ...  </span></li>
<li><span> </span></li>
<li class="alt"><span>Warning: readfile() [function.readfile]: failed to open stream: Invalid argument in .. </span></li>
</ol>
Copy after login

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, the parameters can be converted to the encoding recognized by the operating system. The encoding conversion can be done with mb_convert_encoding (character String, new encoding, original encoding) or iconv (original encoding, new encoding, string), so that the file name saved after processing will not be garbled, and the file can be read normally to achieve uploading and downloading of Chinese name files.

In fact, there is a better solution to PHP Chinese garbled code, which is completely separated from the system, and there is no need to consider what encoding the system is. 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 change the file name to the original name with Chinese characters. Chinese name. The code to implement downloading is as follows

<ol class="dp-xml">
<li class="alt"><span><span>header("Pragma: public");  </span></span></li>
<li><span>header("Expires: 0");  </span></li>
<li class="alt">
<span>header("Cache-Component: must-revalidate, </span><span class="attribute">post-check</span><span>=</span><span class="attribute-value">0</span><span>, </span><span class="attribute">pre-check</span><span>=</span><span class="attribute-value">0</span><span>");  </span>
</li>
<li><span>header("Content-type: $file_type");  </span></li>
<li class="alt"><span>header("Content-Length: $file_size");  </span></li>
<li>
<span>header("Content-Disposition: attachment; </span><span class="attribute">filename</span><span>="$file_name"");  </span>
</li>
<li class="alt"><span>header("Content-Transfer-Encoding: binary");  </span></li>
<li><span>readfile($file_path);  </span></li>
</ol>
Copy after login

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

I hope the above-mentioned solutions to Chinese garbled characters in PHP can help everyone solve the problem.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446277.htmlTechArticle1. 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; char...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 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.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 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.

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.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 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.

See all articles