PHP utf-8 coding problem, UTF8 encoding, database garbled, page display output garbled _php tips

Source: Internet
Author: User

The old voice talks, is confused many people's question, if the processing is not good, are garbled, said these words is not my code is very proficient, just in this respect is to be careful, I summed up a little experience (easy to appear garbled place has PHP file inside, the database stored in the code, page display, data transmission):

1. In the construction of the database, especially with phpMyAdmin and MySQL when dealing with, is generally utf-8, field utf8_general_ci

Database settings:

Find in My.ini file:
[MySQL]
Default-character-set = UTF8
[Mysqld]
Default-character-set = UTF8
Init_connect = ' SET NAMES UTF8 '
All set to UTF8
Save, restart the MySQL service

2. When dealing with the data mysql_query ("Set names ' UTF8 '"); note: UTF8, not utf-8

3.PHP of the 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 coding UTF-8, note: Can not choose: UTF-8 + BOM, if you choose this, You will have problems when dealing with the session, so be sure to pay attention to it. There are some people in eclipse,myeclipse,zendstudio development, eclipse inside the default is iso-8859-1, need in the window-> "preferences" Open the Preferences window, on the left "normal"-> "Workspace" and set the default encoding in "Text file encoding" to Utf-8

4. Is the PHP file inside to explain: such as <meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >

Or

Copy Code code as follows:

<?php header (' Content-type:text/html;charset=utf-8 ');? >

5. There is also the processing of Chinese and other double-byte when there may be garbled, PHP can be used in the iconv,mb_convert_encoding to deal with Double-byte, the rest can refer to the PHP help manual

6. At the point of addition (omission), in your program you need to know the transmission between the data may also have coding problems, but you do not know what to pass the data is used in the code, in PHP provides a method to deal with, the following is a simple way to write their own, you can refer to the

Copy Code code as follows:

Encoding Conversion
function display_fileencoding ($filename)
{
if (extension_loaded ("mbstring"))
{
$code =mb_detect_encoding ($filename);//Detect string encoding
$filename =mb_convert_encoding ($filename, "UTF-8", $code);//Convert encoding $code to UTF-8 encoding
return $filename;
}
Else
Die ("Please check that the system is properly installed and configured mbstring");
}

Make sure mbstring is enabled in your php.ini.

7. Ensure that the above codes are consistent!! Database, Web page output will not appear garbled, if there is wrong, 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.