比如你用Zend Studio或Editplus寫程式時,用的是gbk編碼,如果資料需要入資料庫,而資料庫的編碼為utf8時,這時就要把資料進行編碼轉換,不然進到資料庫就會變成亂碼。
mb_convert_encoding的用法見官方:
mb_convert_encoding — Convert character encoding
Report a bug 說明
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
Converts the character encoding of string str to to_encoding from optionally from_encoding.
Report a bug 參數
str
The string being encoded.
to_encoding
The type of encoding that str is being converted to.
from_encoding
Is specified by character code names before conversion. It is either an array, or a comma separated enumerated list. If from_encoding is not specified, the internal encoding will be used.
See supported encodings.
Report a bug 傳回值
The encoded string.
Report a bug 範例
Example #1 mb_convert_encoding() example
| 代碼如下 |
複製代碼 |
<?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"); ?> |
mb_convert_encoding( $str, $encoding1,$encoding2 )
$str,要轉換編碼的字串
$encoding1,目標編碼,如utf-8,gbk,大小寫均可
$encoding2,原編碼,如utf-8,gbk,大小寫均可
執行個體1
| 代碼如下 |
複製代碼 |
<?php $str='指令碼之家:http://www.111cn.net'; echo mb_convert_encoding($str, "UTF-8"); //編碼轉換為utf-8 ?> |
| 代碼如下 |
複製代碼 |
<?php $str='指令碼之家:http://www.111cn.net'; echo mb_convert_encoding($str, "UTF-8", "GBK"); //已知原編碼為GBK,轉換為utf-8 ?> |
| 代碼如下 |
複製代碼 |
<?php $str='指令碼之家:http://www.111cn.net'; echo mb_convert_encoding($str, "UTF-8", "auto"); //未知原編碼,通過auto自動檢測後,轉換編碼為utf-8 ?> |
做一個GBK To UTF-8
| 代碼如下 |
複製代碼 |
< ?php header("content-Type: text/html; charset=Utf-8"); echo mb_convert_encoding("???S我的友仔", "UTF-8", "GBK"); ?> |
再來個GB2312 To Big5
| 代碼如下 |
複製代碼 |
< ?php header("content-Type: text/html; charset=big5"); echo mb_convert_encoding("你是我的朋友", "big5", "GB2312"); ?> |
不過要使用上面的函數需要安裝但是需要先enable mbstring 擴充庫。
PHP中的另外一個函數iconv也是用來轉換字串編碼的,與上函數功能相似。
下面還有一些詳細的例子:
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)
用法:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
需要先enable mbstring 擴充庫,在 php.ini裡將; extension=php_mbstring.dll 前面的 ; 去掉
mb_convert_encoding 可以指定多種輸入編碼,它會根據內容自動識別,但是執行效率比iconv差太多;
string iconv ( string in_charset, string out_charset, string str )
注意:第二個參數,除了可以指定要轉化到的編碼以外,還可以增加兩個尾碼://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 會自動將不能直接轉化的字元變成一個或多個近似的字元,//IGNORE 會忽略掉不能轉化的字元,而預設效果是從第一個非法字元截斷。
Returns the converted string or FALSE on failure.
使用:
發現iconv在轉換字元”—”到gb2312時會出錯,如果沒有ignore參數,所有該字元後面的字串都無法被儲存。不管怎麼樣,這個”—”都無法轉換成功,無法輸出。 另外mb_convert_encoding沒有這個bug.
一般情況下用 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//IGNORE″, $content); $content = mb_convert_encoding($content, “UTF-8″, “GBK”); |
一般情況下用 iconv,只有當遇到無法確定原編碼是何種編碼,或者iconv轉化後無法正常顯示時才用mb_convert_encoding 函數