mb_convert_encoding這個函數是用來轉換編碼的。原來一直對程序編碼這一概念不理解,不過現在好像有點開竅了。
不過英文一般不會存在編碼問題,只有中文數據才會有這個問題。比如你用Zend Studio或Editplus寫程序時,用的是gbk編碼,如果數據需要入數據庫,而數據庫的編碼為utf8時,這時就要把數據進行編碼轉換,不然進到數據庫就會變成亂碼。
mb_convert_encoding的用法見官方:
做一個GBK To UTF-8
復制代碼 代碼如下:
< ?php
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");
?>
復制代碼 代碼如下:
< ?php
header("content-Type: text/html; charset=big5");
echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");
?>
復制代碼 代碼如下:
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)
一般情況下用 iconv,只有當遇到無法確定原編碼是何種編碼,或者iconv轉化后無法正常顯示時才用mb_convert_encoding 函數.
復制代碼 代碼如下:
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”);
復制代碼 代碼如下:
$content = iconv(”GBK”, “UTF-8″, $content);
$content = mb_convert_encoding($content, “UTF-8″, “GBK”);
新聞熱點
疑難解答