PHP Chinese garbled solution

Source: Internet
Author: User
PHP + MYSQL for website development usually encounters garbled characters when the browser outputs Chinese characters. this problem is mainly caused by inconsistent HTML content encoding, PHP file encoding, and MySQL database encoding. Today, we will discuss the solutions for garbled Chinese characters. Chinese character garbled characters are a sad reminder. JAVA hates Chinese characters, and PHP does not like Chinese characters;

Java garbled characters are finally filtered using the filters provided by spring. everywhere filtering actually affects the speed, but there is no way. Chinese characters are the first things that W countries do not consider;

Unexpectedly, PHP is everywhere without code. when you use MySQL, the Chinese character looks so friendly that you never think of it as a book. but to interact with other users, when we put PHP's hand into SQL SERVER, garbled characters are generated because the GBK encoding is used by a third-party system;

Ah, switch;

1. PHP built-in conversion function ICONV, a tall function;

The code is as follows:


String iconv (string $ in_charset, string $ out_charset, string $ str)

Use DEMO:

The code is as follows:


<? Php
$ Text = "This is the Euro Symbol '? '.";
Echo 'original: ', $ text, PHP_EOL;
Echo 'transcoder: ', iconv ("UTF-8", "ISO-8859-1 // transcoder", $ text), PHP_EOL;
Echo 'ignore: ', iconv ("UTF-8", "ISO-8859-1 // IGNORE", $ text), PHP_EOL;
Echo 'plain: ', iconv ("UTF-8", "ISO-8859-1", $ text), PHP_EOL;
?>

Functions recommended by everyone, but cannot be converted after use, NO errors, NO conversion of characters, NO!

2. Another way is to find a function that is not highly efficient. However, the other three functions can be implemented first.

The code is as follows:


// Check whether the function is available
Echo function_exists ('MB _ convert_encoding ');
// Check the current encoding
Echo mb_detect_encoding ($ val, "GBK, GB2312, UTF-8 ");
// Convert the CP936 (GBK) into a UTF-8
$ V = mb_convert_encoding ($ val, "UTF-8", "CP936 ");

The result is successful;

Well, use it first. to convert the database query result set, create a conversion function:

1. function "garbled Dipper ":

The code is as follows:


// $ FContents string
// $ From string encoding
// $ To encoding to be converted
Function auto_charset ($ fContents, $ from = 'gbk', $ to = 'utf-8 '){
$ From = strtoupper ($ from) = 'utf8 '? 'Utf-8': $ from;
$ To = strtoupper ($ to) = 'utf8 '? 'Utf-8': $;
If (strtoupper ($ from) === strtoupper ($ to) | empty ($ fContents) | (is_scalar ($ fContents )&&! Is_string ($ fContents ))){
// If the encoding is the same or the non-string scalar, the conversion is not performed.
Return $ fContents;
}
If (is_string ($ fContents )){
If (function_exists ('MB _ convert_encoding ')){
Return mb_convert_encoding ($ fContents, $ to, $ from );
} Else {
Return $ fContents;
}
}
Elseif (is_array ($ fContents )){
Foreach ($ fContents as $ key => $ val ){
$ _ Key = auto_charset ($ key, $ from, $ );
$ FContents [$ _ key] = auto_charset ($ val, $ from, $ );
If ($ key! = $ _ Key)
Unset ($ fContents [$ key]);
}
Return $ fContents;
}
Else {
Return $ fContents;
}
}

2. use:

The code is as follows:


// Print the output query result (assuming your result)
$ Arr = array ();
While ($ list = mssql_fetch_row ($ row ))
{
$ Arr [] = $ list;
}
$ S = auto_charset ($ arr, 'gbk', 'utf-8 ');
// Print try, set the encoding in the browser to UFT-8, see no garbled
Print_r ($ s); die ();

The above is an introduction to php Chinese garbled characters in this article. I hope you will like it.

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.