PHP programming-PHP Chinese garbled solution

Source: Internet
Author: User
PHP Chinese garbled text is one of the common problems in PHP programming. Chinese garbled text sometimes occurs on the web page, and some occur in the MYSQL interaction process, sometimes related to the operating system.

PHP Chinese garbled text is one of the common problems in PHP programming. Chinese garbled text sometimes occurs on the web page. Some are generated during MYSQL interaction and sometimes related to the operating system. The following is a summary.

I. PHP source code
The best and fastest solution is to make the page declarative encoding consistent with the internal encoding of the database. if the requested encoding is inconsistent with the internal encoding of the database, set the connection encoding. Mysql_query ("set names ***").

1. the php file encoding should match the webpage encoding. if you want to use gb2312 encoding, php needs to output the header.
Header ("Content-type: text/html; charset = gb2312 ")
Add static pages
The encoding format of all files is ANSI, which can be opened in Notepad. save it as ANSI and overwrite the source file.

2. if you want to use uft-8 encoding, php needs to output the header
Header ("Content-type: text/html; charset = utf-8 "),
Add static pages
The encoding format of all files is utf-8. it may be a little troublesome to save it as UTF-8. BOM is usually started with UTF-8. if you use Session, problems may occur. you can use editplus to save it. In this software, select a tool? Parameter selection? File? UTF-8 signature, choose always delete, and then save to remove the BOM information.
3. PHP itself is not Unicode. all functions such as substr must be changed to mb_substr (mbstring extension is required), or iconv transcoding is used.

Add mysql_query ("set names encoding") before the PHP program that requires database operations. the encoding is consistent with the PHP code. if the PHP code is gb2312, then the mysql code is gb2312, if it is uft-8, then mysql is utf8. so after the change will not appear garbled.

From battlefield diaries _ LAMP brothers

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.