在php中編碼一直是開發人員的頭痛的事情,但是如果甜美一些有用的函數就不一樣了,下面我們介紹一下關於一個中文編碼的處理函數。
mb_convert_encoding( $str, $encoding1,$encoding2 )
$str,要轉換編碼的字符串
$encoding1,目標編碼,如utf-8,gbk,大小寫均可
$encoding2,原編碼,如utf-8,gbk,大小寫均可
實例1
代碼如下 復制代碼<?php
$str='電影618:http://www.bKjia.c0m';
echo mb_convert_encoding($str, "UTF-8"); //編碼轉換為utf-8
?>
實例2
代碼如下 復制代碼<?php
$str='電影618:http://www.bKjia.c0m';
echo mb_convert_encoding($str, "UTF-8", "GBK"); //已知原編碼為GBK,轉換為utf-8
?>
實例3
代碼如下 復制代碼<?php
$str='電影618:http://www.bKjia.c0m';
echo mb_convert_encoding($str, "UTF-8", "auto"); //未知原編碼,通過auto自動檢測後,轉換編碼為utf-8
?>
php.net網站實例
代碼如下 復制代碼
<?php
/* 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");
?>