PHP Chinese garbled solution

Source: Internet
Author: User
Tags php file translit mysql database

Php+mysql do web site development will usually encounter the browser output Chinese characters garbled, this problem is mainly because of HTML content encoding, PHP file encoding and MySQL database coding these three inconsistencies caused. Today we have to specifically explore the Chinese garbled after the solution.

-->

Chinese characters garbled is really a sad reminders of things, Java hate Chinese characters, PHP also does not like Chinese characters;

Java garbled finally used the filter to filter the spring, everywhere filtering, in fact, affect the speed, but there is no way, the Chinese character is the first thing not to consider the W country;

Can not think of PHP is garbled everywhere, when you use your brother MySQL, the Chinese characters appear so cordial, never considered he will become a heavenly book; but in order to and other interaction, the PHP hand to SQL Server, garbled came, because the third party system with the GBK code;

Ah, convert it;

1,php transform 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 ' 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;
?>

All recommended functions, but can not be converted after use, no errors, characters are not converted, no!

2, another way, there is a very inefficient function, but in any case, first to consider the other three

The code is as follows:
Check to see if the function is available
echo function_exists (' mb_convert_encoding ');
Detecting Current encoding
Echo mb_detect_encoding ($val, "GBK, GB2312, UTF-8");
Convert code to convert CP936 (GBK) to UTF-8
$v =mb_convert_encoding ($val, "UTF-8", "CP936");

The results were successful;

Well, use it first, in order to transform the result set of the database query, make a transformation function:

1, function "garbled nemesis":

The code is as follows:
$fContents string
Encoding of $from strings
$to the 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)) {
Does not convert if the encoding is the same or is not a string scalar
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:

The code is as follows:
Print output query results (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 ();

The above mentioned is this article about the PHP Chinese garbled introduction, I hope you can like.

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.