Home >Backend Development >PHP Tutorial >PHP Chinese garbled solution, _PHP tutorial

PHP Chinese garbled solution, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:26881browse

PHP Chinese garbled solution,

Garbled Chinese characters is really a sad thing, JAVA hates Chinese characters, and PHP doesn’t like Chinese characters either;

Java garbled code was finally filtered using the filter provided by spring. Filtering everywhere actually affects the speed, but there is no way. Chinese characters are the first thing that W country does not consider;

I didn’t expect that PHP is also full of garbled characters. When you use my brother MySQL, the Chinese characters seem so friendly. I never thought that they would become a bible. However, in order to interact with others, I put PHP’s hand into the SQL SERVER. At that time, garbled codes came. The reason was the GBK encoding used by the third-party system;

Hey, switch;

1. PHP’s own conversion function ICONV, a sophisticated function;

Copy code The code is as follows:
string iconv ( string $in_charset , string $out_charset , string $str )

Use DEMO:

Copy code The code is as follows:
$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;
?>

A function that everyone recommends, but it cannot be converted after use. There are no errors and the characters are not converted. NO!

2. Take another approach, and there is another function that everyone doubts is inefficient, but in any case, implement it first and then consider the other three

Copy code The code is as follows:
//Check whether the function is available
echo function_exists('mb_convert_encoding');
//Detect current encoding
echo mb_detect_encoding($val, "GBK, GB2312, UTF-8");
//Convert encoding, convert CP936 (that is, GBK) to UTF-8
$v=mb_convert_encoding ($val, "UTF-8", "CP936");

The result was successful;

Okay, let’s use it first. In order to convert the result set of the database query, make a conversion function:

1. Function "garbled nemesis":

Copy code The code is as follows:
// $fContents string
// $from string 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 it is not a string scalar, it will not be 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, use:

Copy code The code is as follows:
//Print the query results (assuming your results)
$arr=array();
while($list=mssql_fetch_row($row))
{
$arr[]=$list;
}
$s=auto_charset($arr,'gbk','utf-8');
//Try printing, set the encoding to UFT-8 in the browser, and see that there are no garbled characters
print_r($s);die();

The above is the introduction of this article about Chinese garbled characters in PHP. I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963843.htmlTechArticlePHP Chinese garbled solution, garbled Chinese characters is really a sad thing, JAVA hates Chinese characters, and PHP doesn’t like Chinese characters either; Java garbled code was finally filtered using the filter provided by spring, everywhere...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn