PHP also has the evil Chinese garbled, the solution

Source: Internet
Author: User
Tags translit

Chinese characters garbled is really a sad thing, Java hate Chinese characters, PHP also do not like Chinese characters;

Java garbled finally use the spring to filter, everywhere filter, in fact, affect the speed, but there is no way, the Chinese character is the first thing that W state does not consider;

Unexpectedly PHP is also garbled everywhere in, when you use the brother MySQL, the Chinese character seems so cordial, never considered he will become a heavenly book, but in order to and other interactions, the PHP hand stretched to SQL Server, garbled, because the third-party system used by the GBK encoding;

Hey, convert it;

1,php's own conversion function iconv, a tall function;

String Iconv (String $in _charset, String $out _charset, String $str)

Use Demo:

<?php$text = "This is the Euro symbol ' € '.";    Echo ' Original: ', $text, Php_eol;echo ' translit: ', Iconv ("UTF-8", "Iso-8859-1//translit", $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;? >

We recommend the function, but can not be converted after use, there is no error, the character is not converted, no!

2, another way, there is a question of the efficiency of the function is not high, but in any case, first of all, consider the other three

Check if the function is available in echo function_exists (' mb_convert_encoding ');//Detect Current Code echo mb_detect_encoding ($val, "GBK, GB2312, UTF-8") ;//conversion code, the CP936 (is GBK) converted to utf-8$v=mb_convert_encoding ($val, "UTF-8", "CP936");

The result was successful;

All right, let's use it. To convert the result set of a database query, make a conversion function:

1, function "Garbage buster":

  $fContents   String//  $from   encoding//  $to   encoding to convert 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 the non-string scalar is not converted         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, $to);              $fContents [$_key] = auto_charset ($val, $from, $to);             if ($key  != $_key )                  unset ($fContents [$key]);         }        return  $fContents;     }    else{         return  $fContents;     }}

2, using:

Print out the results of the query (assuming your results) $arr =array (), while ($list =mssql_fetch_row ($row)) {$arr []= $list;} $s =auto_charset ($arr, ' gbk ', ' utf-8 ');//print try, in the browser set code for UFT-8, see no garbled print_r ($s);d ie ();


PHP also has the evil Chinese garbled, the solution

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.