Home >Backend Development >PHP Tutorial >PHP Chinese encoding conversion mb_convert_encoding() function_PHP tutorial

PHP Chinese encoding conversion mb_convert_encoding() function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:55:211119browse

Coding in PHP has always been a headache for developers, but it would be different if there were some sweet and useful functions. Let’s introduce a processing function for Chinese encoding.

mb_convert_encoding( $str, $encoding1,$encoding2 )

$str, the string to be converted to encoding
$encoding1, target encoding, such as utf-8, gbk, both upper and lower case
$encoding2, original encoding, such as utf-8, gbk, both upper and lower case

Example 1

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
The code is as follows
 代码如下 复制代码

$str='电影618:http://www.bKjia.c0m';
echo mb_convert_encoding($str, "UTF-8"); //编码转换为utf-8
?> 

Copy code

 代码如下 复制代码

$str='电影618:http://www.bKjia.c0m';
echo mb_convert_encoding($str, "UTF-8", "GBK"); //已知原编码为GBK,转换为utf-8
?> 

$str='Movie 618:http://www.bKjia.c0m';

echo mb_convert_encoding($str, "UTF-8"); //Encoding converted to utf-8
代码如下 复制代码

$str='电影618:http://www.bKjia.c0m';
echo mb_convert_encoding($str, "UTF-8", "auto"); //未知原编码,通过auto自动检测后,转换编码为utf-8
?>

?>

 代码如下 复制代码

/* Convert internal character encoding to SJIS */
$str = mb_convert_encoding($str, "SJIS");

/* Convert EUC-JP to UTF-7 */
$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");

/* 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 2
The code is as follows
Copy code

$str='Movie 618:http://www.bKjia.c0m'; ?> Example 3

The code is as follows Copy code
$str='Movie 618:http://www.bKjia.c0m';

echo mb_convert_encoding($str, "UTF-8", "auto"); //Unknown original encoding, after automatic detection by auto, convert the encoding to utf-8 ?>
php.net website example
The code is as follows Copy code
/* Convert internal character encoding to SJIS */<🎜> $str = mb_convert_encoding($str, "SJIS");<🎜> <🎜>/* Convert EUC-JP to UTF-7 */<🎜> $str = mb_convert_encoding($str, "UTF-7", "EUC-JP");<🎜> <🎜>/* 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");<🎜> ?> http://www.bkjia.com/PHPjc/632258.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632258.htmlTechArticleCoding in PHP has always been a headache for developers, but it will be different if some useful functions are added. , below we introduce the processing function of a Chinese encoding. ...