PHP also has all the evil Chinese garbled characters. the solution to Chinese garbled characters is really 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;
string iconv ( string $in_charset , string $out_charset , string $str )
Use DEMO:
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.
// 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) to 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 ":
// $ FContents string // $ from string encoding // $ to function auto_charset ($ fContents, $ from = 'gbk ', $ to = 'utf-8') {$ from = strtoupper ($ from) = 'utf8 '? 'Utf-8': $ from; $ to = strtoupper ($ to) = 'utf8 '? 'Utf-8': $ to; if (strtoupper ($ from) === strtoupper ($ to) | empty ($ fContents) | (is_scalar ($ fContents) &&! Is_string ($ fContents) {// if the encoding is the same or non-string scalar, return $ fContents is not converted;} 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, $ to); if ($ key! =$ _ Key) unset ($ fContents [$ key]);} return $ fContents;} else {return $ fContents ;}}
2. use:
// 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 it and set it to UFT-8 in the browser to see no garbled print_r ($ s ); die ();