PHP Chinese garbled solution, _php tutorial

Source: Internet
Author: User
Tags translit

PHP Chinese garbled solution,


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;

Copy the Code code as follows:
String Iconv (String $in _charset, String $out _charset, String $str)

Use Demo:

Copy the Code code 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;
?>

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

Copy the Code code as follows:
Check if the function is available
echo function_exists (' mb_convert_encoding ');
Detect current encoding
Echo mb_detect_encoding ($val, "GBK, GB2312, UTF-8");
Convert the code to convert CP936 (that is, GBK) 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":

Copy the Code code 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))) {
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:

Copy the Code code 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, set the code in the browser to UFT-8, see no garbled
Print_r ($s);d ie ();

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

http://www.bkjia.com/PHPjc/963843.html www.bkjia.com true http://www.bkjia.com/PHPjc/963843.html techarticle PHP Chinese garbled solution, Chinese character garbled is really a sad thing, Java hate Chinese characters, PHP also do not like Chinese characters; Java garbled finally used the spring to filter the filter, everywhere ...

  • Related Article

    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.