Home  >  Article  >  Backend Development  >  PHP character encoding conversion from gb2312 to utf8_PHP tutorial

PHP character encoding conversion from gb2312 to utf8_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:17856browse

In character encoding conversion in PHP, we generally use iconv and mb_convert_encoding to operate, but the conversion performance of mb_convert_encoding is much worse than iconv.

string iconv ( string in_charset, string out_charset, string str )
Note: In addition to specifying the encoding to be converted to, the second parameter can also add two suffixes: //TRANSLIT and //IGNORE, where //TRANSLIT will automatically convert characters that cannot be directly converted into one or more Approximate characters, //IGNORE will ignore characters that cannot be converted, and the default effect is to truncate from the first illegal character.
Returns the converted string or FALSE on failure.

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
You need to enable the mbstring extension library first, and remove the ; in front of extension=php_mbstring.dll in php.ini
mb_convert_encoding can specify multiple input encodings. It will automatically identify based on the content, but the execution efficiency is much worse than iconv;

Use:

It was found that iconv would make an error when converting the character "-" to gb2312. Without the ignore parameter, all strings following this character cannot be saved. No matter what, this "—" cannot be converted successfully and cannot be output. In addition, mb_convert_encoding does not have this bug.

In general, use iconv. Only use the mb_convert_encoding function when you are unable to determine what the original encoding is, or when iconv cannot be displayed normally after conversion.

The code is as follows Copy code
 代码如下 复制代码

/**
*自动判断把gbk或gb2312编码的字符串转为utf8
*能自动判断输入字符串的编码类,如果本身是utf-8就不用转换,否则就转换为utf-8的字符串
*支持的字符编码类型是:utf-8,gbk,gb2312
*@$str:string 字符串
*/
function yang_gbk2utf8($str){
    $charset = mb_detect_encoding()($str,array('UTF-8','GBK','GB2312'));
    $charset = strtolower($charset);
    if('cp936' == $charset){
        $charset='GBK';
    }
    if("utf-8" != $charset){
        $str = iconv($charset,"UTF-8//IGNORE",$str);
    }
    return $str;
}

/**

* Automatically determine and convert gbk or gb2312 encoded strings to utf8

*Can automatically determine the encoding type of the input string. If it is UTF-8, there is no need to convert it. Otherwise, it will be converted to a UTF-8 string

*Supported character encoding types are: utf-8, gbk, gb2312

*@$str:string string
 代码如下 复制代码
$str="测试ing";
$cha=mb_detect_encoding($str);
echo $cha;
?>
*/

function yang_gbk2utf8($str){

$charset = mb_detect_encoding()($str,array('UTF-8','GBK','GB2312'));

$charset = strtolower($charset);

If('cp936' == $charset){
 代码如下 复制代码
$str="测试ing";
$cha=mb_detect_encoding($str);
$s = iconv($cha,"UTF-8",$str);
var_dump($s);
?>
         $charset='GBK'; } If("utf-8" != $charset){            $str = iconv($charset,"UTF-8//IGNORE",$str); } Return $str; }
Now I will look at some problems in converting character encoding Use the mb_detect_encoding($str); function. To use this function, you must open the php extension=php_mbstring.dll extension
The code is as follows Copy code
$str="Testing";<🎜> $cha=mb_detect_encoding($str);<🎜> echo $cha;<🎜> ?>
I entered it on the gb2312 page, but the output result is UTF-8, which is very strange, and I haven’t found the reason yet. I want to uniformly convert to UTF-8 encoding, use the following method
The code is as follows Copy code
$str="Testing";<🎜> $cha=mb_detect_encoding($str);<🎜> $s = iconv($cha,"UTF-8",$str);<🎜> var_dump($s);<🎜> ?>

Result returned:
string(0) “”
It's really strange why this happens.
Use

The code is as follows Copy code
代码如下 复制代码
$str="测试ing";
$cha=mb_detect_encoding($str);
$s = iconv("GB2312","UTF-8",$str);
var_dump($s);
?>
$str="Testing";

$cha=mb_detect_encoding($str);
$s = iconv("GB2312","UTF-8",$str);
var_dump($s);
?>

 代码如下 复制代码

$a="我很好";
echo mb_convert_encoding ($a,'UTF-8');
?>
The returned result is correct. It is found that the function mb_detect_encoding($str); is still inaccurate. I don't know why.

Function string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )

Can be converted to a string with a specified encoding, I wrote an example

The code is as follows Copy code

$a="I'm fine";

echo mb_convert_encoding ($a,'UTF-8');

?>

 代码如下 复制代码
$str=chr(254)."测试ing".chr(254);
$s = iconv("GB2312","UTF-8",$str);
var_dump($s);
?>
The result is: ??枞?枞? The question now is if I convert different string encodings to UTF-8, can I use iconv if I know the change in advance? But what should I do if I don’t know the encoding? Problem 3: iconv problem, if the encoding of the first byte of the converted string is greater than a certain number, null will be returned. For example:
The code is as follows Copy code
$str=chr(254)."Testing".chr(254);<🎜> $s = iconv("GB2312","UTF-8",$str);<🎜> var_dump($s);<🎜> ?>

Return
string(0) “”

For the usage of mb_convert_encoding, please see the official website:

http://cn.php.net/manual/en/function.mb-convert-encoding.php

Another function iconv in PHP is also used to convert string encoding, and its function is similar to the function above.

There are some detailed examples below:
iconv — Convert string to requested character encoding
(PHP 4 >= 4.0.5, PHP 5)
mb_convert_encoding — Convert character encoding
(PHP 4 >= 4.0.6, PHP 5)

Usage:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
You need to enable the mbstring extension library first, and remove the ; in front of extension=php_mbstring.dll in php.ini
mb_convert_encoding can specify multiple input encodings. It will automatically identify based on the content, but the execution efficiency is much worse than iconv;

string iconv (string in_charset, string out_charset, string str)
Note: In addition to specifying the encoding to be converted to, the second parameter can also add two suffixes: //TRANSLIT and //IGNORE, where //TRANSLIT will automatically convert characters that cannot be directly converted into one or more Approximate characters, //IGNORE will ignore characters that cannot be converted, and the default effect is to truncate from the first illegal character.
Returns the converted string or FALSE on failure.

Use:
It was found that iconv would make an error when converting the character "-" to gb2312. Without the ignore parameter, all strings following this character cannot be saved. No matter what, this "—" cannot be converted successfully and cannot be output. In addition, mb_convert_encoding does not have this bug.
Under normal circumstances, use iconv. Only use the mb_convert_encoding function when you cannot determine what the original encoding is, or when iconv cannot be displayed normally after conversion.

from_encoding is specified by character code name before conversion. it can be array or string – comma separated enumerated list. If it is not specified, the internal encoding will be used.
/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
$str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”);
/* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */
$str = mb_convert_encoding($str, “EUC-JP”, “auto”);

Example:

The code is as follows
 代码如下 复制代码

$content = iconv("GBK", "UTF-8", $content);
$content = mb_convert_encoding($content, "UTF-8", "GBK");
?>

Copy code

$content = iconv("GBK", "UTF-8", $content);

代码如下 复制代码

function phpcharset($data, $to) {
if(is_array($data)) {
foreach($data as $key => $val) {
   $data[$key] = phpcharset($val, $to);
  }
 } else {
  $encode_array = array('ASCII', 'UTF-8', 'GBK', 'GB2312', 'BIG5');
  $encoded = mb_detect_encoding($data, $encode_array);
  $to = strtoupper($to);
  if($encoded != $to) {
   $data = mb_convert_encoding($data, $to, $encoded);
  }
 }
 return $data;
}
?>

$content = mb_convert_encoding($content, "UTF-8", "GBK"); ?>

ExampleThis can be converted according to the input and output character encoding
The code is as follows Copy code
function phpcharset($data, $to) { if(is_array($data)) { foreach($data as $key => $val) { $data[$key] = phpcharset($val, $to); } } else {

$encode_array = array('ASCII', 'UTF-8', 'GBK', 'GB2312', 'BIG5'); $encoded = mb_detect_encoding($data, $encode_array);
$to = strtoupper($to);
if($encoded != $to) {<🎜> $data = mb_convert_encoding($data, $to, $encoded);<🎜> }<🎜> }<🎜> return $data;<🎜> }<🎜> ?> http://www.bkjia.com/PHPjc/632791.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632791.htmlTechArticleIn character encoding conversion in php, we generally use iconv and mb_convert_encoding to operate, but mb_convert_encoding has better conversion performance than iconv is much different. string iconv ( string...
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