Home >Backend Development >PHP Tutorial >PHP character encoding conversion from gb2312 to utf8_PHP tutorial
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 | ||||||||||||
* 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
function yang_gbk2utf8($str){ $charset = mb_detect_encoding()($str,array('UTF-8','GBK','GB2312'));$charset = strtolower($charset); If('cp936' == $charset){
|
The code is as follows | Copy code |
$str="Testing";<🎜> $cha=mb_detect_encoding($str);<🎜> echo $cha;<🎜> ?> |
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); ?> |
$cha=mb_detect_encoding($str);
$s = iconv("GB2312","UTF-8",$str);
var_dump($s);
?>
代码如下 | 复制代码 |
|
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 | ||||
|
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
|
Copy code
|
||||
$content = iconv("GBK", "UTF-8", $content);
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 Previous article:Several examples of bubble sorting implemented in PHP_PHP tutorialNext article:Several examples of bubble sorting implemented in PHP_PHP tutorial Related articlesSee more |