PHP中mb_convert_encoding與iconv函數(shù)的深入解析
mb_convert_encoding這個函數(shù)是用來轉換編碼的。原來一直對程序編碼這一概念不理解,不過現(xiàn)在好像有點開竅了。
不過英文一般不會存在編碼問題,只有中文數(shù)據(jù)才會有這個問題。比如你用Zend Studio或Editplus寫程序時,用的是gbk編碼,如果數(shù)據(jù)需要入數(shù)據(jù)庫,而數(shù)據(jù)庫的編碼為utf8時,這時就要把數(shù)據(jù)進行編碼轉換,不然進到數(shù)據(jù)庫就會變成亂碼。
mb_convert_encoding的用法見官方:
http://php.net/manual/zh/function.mb-convert-encoding.php
做一個GBK To UTF-8
< ?php
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");
?>
再來個GB2312 To Big5
< ?php
header("content-Type: text/html; charset=big5");
echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");
?>
不過要使用上面的函數(shù)需要安裝但是需要先enable mbstring 擴展庫。
PHP中的另外一個函數(shù)iconv也是用來轉換字符串編碼的,與上函數(shù)功能相似。
下面還有一些詳細的例子:
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 可以指定多種輸入編碼,它會根據(jù)內容自動識別,但是執(zhí)行效率比iconv差太多;
string iconv ( string in_charset, string out_charset, string str )
注意:第二個參數(shù),除了可以指定要轉化到的編碼以外,還可以增加兩個后綴://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 會自動將不能直接轉化的字符變成一個或多個近似的字符,//IGNORE 會忽略掉不能轉化的字符,而默認效果是從第一個非法字符截斷。
Returns the converted string or FALSE on failure.
使用:
發(fā)現(xiàn)iconv在轉換字符”—”到gb2312時會出錯,如果沒有ignore參數(shù),所有該字符后面的字符串都無法被保存。不管怎么樣,這個”—”都無法轉換成功,無法輸出。 另外mb_convert_encoding沒有這個bug.
一般情況下用 iconv,只有當遇到無法確定原編碼是何種編碼,或者iconv轉化后無法正常顯示時才用mb_convert_encoding 函數(shù).
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”);
- php中iconv函數(shù)使用方法
- PHP iconv 函數(shù)轉gb2312的bug解決方法
- PHP下編碼轉換函數(shù)mb_convert_encoding與iconv的使用說明
- PHP iconv 解決utf-8和gb2312編碼轉換問題
- php iconv() : Detected an illegal character in input string
- PHP通過iconv將字符串從GBK轉換為UTF8字符集
- linux系統(tǒng)上支持php的 iconv()函數(shù)的方法
- php 轉換字符串編碼 iconv與mb_convert_encoding的區(qū)別說明
- 基于php iconv函數(shù)的使用詳解
- php截取字符串函數(shù)substr,iconv_substr,mb_substr示例以及優(yōu)劣分析
- PHP中iconv函數(shù)知識匯總
相關文章
php max_execution_time執(zhí)行時間問題
大部分PHP代碼執(zhí)行時間都不會很久。但是有些時候,比如等待圖片上傳,可能執(zhí)行時間過長導致超時。2011-07-07
php strlen mb_strlen計算中英文混排字符串長度
在php中常見的計算字符串長度的函數(shù)有:strlen和mb_strlen,下面是對這兩個函數(shù)的比較說明(編碼方式UTF8)2009-07-07
php下通過curl抓取yahoo boss 搜索結果的實現(xiàn)代碼
php下通過curl抓取yahoo boss 搜索結果的實現(xiàn)代碼,需要的朋友可以參考下。2011-06-06
php實現(xiàn)的click captcha點擊驗證碼類實例
這篇文章主要介紹了php實現(xiàn)的click captcha點擊驗證碼類實例,不同于以往傳統(tǒng)的驗證碼,該驗證碼類可實現(xiàn)手機用戶點擊某一位置確認驗證碼,非常實用,需要的朋友可以參考下2014-09-09

