PHP UTF-8 encoding, utf8 encoding, database garbled characters, and output garbled characters on the page

Source: Internet
Author: User

In the long talk, I am confused about many people's problems. If they are not well handled, they are all garbled characters. I am not very proficient in coding, but I have to be careful in this regard, I have summarized a little experience (PHP files are prone to garbled characters, encoding stored in the database, page display, and data transmission ):

1. During database creation, especially when phpMyAdmin is used to deal with MYSQL, it is generally UTF-8 and the field is utf8_general_ci

Database settings:

Find in the my. ini file:
[Mysql]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8
Init_connect = 'set NAMES utf8'
Set all to utf8
Save and restart the mysql Service

2. When dealing with data, mysql_query ("set names 'utf8'"); Note: utf8 is not UTF-8.

3. PHP file default encoding is ANSI, need to convert to UTF-8, as to how to convert editplus has such a function, "Save as" when there is a choice of encoding UTF-8, note: can not choose: UTF-8 + BOM, if you choose this, you will encounter problems when processing session, so be sure to pay attention. there are some people in eclipse, Myeclipse, ZendStudio development, eclipse inside the default is the ISO-8859-1, need to in the "window"-> "Preferences" to open the "Preferences" window, in "regular"-> "workspace" on the left, set the default encoding to UTF-8 in "text file encoding ".

4. The php file should be described as follows: <meta http-equiv = "Content-Type" Content = "text/html; charset = UTF-8">

Or

Copy codeThe Code is as follows: <? Php header ('content-Type: text/html; charset = UTF-8 ');?>

5. garbled characters may also occur when processing Chinese characters and other double bytes. iconv and mb_convert_encoding can be used in PHP to process Double Bytes. For other data, see the PHP help manual.

6. in addition, you need to know in your program that data transmission may also have Encoding Problems, however, you don't know what encoding is used for the transmitted data. In PHP, the method is provided for processing. The following is a simple method written by yourself. For more information, see

Copy codeThe Code is as follows: // encoding conversion
Function display_fileencoding ($ filename)
{
If (extension_loaded ("mbstring "))
{
$ Code = mb_detect_encoding ($ filename); // checks the string encoding.
$ Filename = mb_convert_encoding ($ filename, "UTF-8", $ code); // convert encoding $ code to UTF-8 encoding
Return $ filename;
}
Else
Die ("check whether the system is correctly installed and configured with mbstring ");
}

Make sure that mbstring is enabled in your php. ini

7. Make sure the above encoding is consistent !! Database and webpage output will not be garbled. If there is any error, please leave a message !!

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.