PHP garbled code Ultimate Solution

Source: Internet
Author: User

In PHP programming, it is a headache to deal with Garbled text. The following is a summary of my experience in handling Garbled text in PHP tutorials I have studied these days. I hope it will be useful to you.

Garbled characters are mostly caused by different codes. In case of garbled characters, you must first find the solution from the encoding. The important point is that all files must be encoded in a unified manner, for example, database encoding, page encoding, and template encoding must be consistent;
1. Database encoding settings:
(1) Use the navicat tool to open the database connection, right-click the database to modify the encoding-database attributes, and select the encoding format. Note: In the figure is the selected UTF-8 format, the actual application to select the desired format, sometimes the same format also has a variety, to treat differently, for example, utf8 has two scenarios: BOM and BOM.

(2) If the navicat tool is not installed, 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

Add the following under [mysqld:
Default-collation = utf8_bin
Init_connect = 'set names utf8' Note: It is utf8 instead of UTF-8.
(3) add mysql_query ("set names 'encoding '") before the PHP program that requires database operations. The encoding is consistent with the PHP code, if php encoding is gb2312, MySQL encoding is gb2312. If it is UTF-8, MySQL encoding is utf8. In this way, no garbled characters will occur during data insertion or retrieval.

2. Page code:
(1) the encoding of the page declaration 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 is garbled. More often it is caused by minor bugs fixed after the release, opening the page with error code, and saving it. Or you can use some FTP software to directly modify files online, such as CuteFTP. The conversion error is caused by incorrect software encoding.
(2) In Dreamweaver, you can modify the page encoding in "menu"-"modify"-"Page attribute. Do not select "include Unicode signature.

(2) Add the header ('content-type: text/html; charset = UTF-8 ') at the beginning of the PHP page ');
(3) On the HTML page, add <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> to the document, this code can also be added to the top of the pure PHP page.
[Note: You can set the encoding of the "edit"-"Preferences"-"New Document" item in Dreamweaver to ensure the unified encoding of each newly created document.]

3. In Windows, the encoding of the document directly stored in text files is GBK. Therefore, you can create a document directly by using text files. It is best to save the file as a file with the same encoding, you can also set page properties in Dreamweaver and save the settings.
[Note: Changing the encoding may cause garbled characters in the source file's existing Chinese characters. Modify the Chinese characters in the source file.]

4. Some friends who rent a VM clearly confirm that the above three codes are correctly set and there are still garbled characters. For example, the web page is gb2312 encoding, ie and other browsers open but always recognized as a UTF-8, the web page head has been declared is gb2312, manually modify the browser code to gb2312 after the page shows normal. The cause is that the server Apache sets the server's global default encoding, added the adddefaultcharset UTF-8 in httpd. conf. At this time, the server will first send an HTTP header to the browser, which has a higher priority than the declarative encoding in the page, and the natural browser will recognize the error. There are two solutions. The administrator needs to add an adddefacharcharset gb2312 to the virtual machine in the configuration file to overwrite the global configuration, or configure it in the. htaccess directory.
The suggestion here is that our encoding should adopt the UTF-8 without BOM encoding method as much as possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.