php 轉(zhuǎn)換字符串編碼 iconv與mb_convert_encoding的區(qū)別說(shuō)明
更新時(shí)間:2011年11月10日 14:25:56 作者:
php 轉(zhuǎn)換字符串編碼 iconv與mb_convert_encoding的區(qū)別說(shuō)明,需要的朋友可以參考下。
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] )
需要先啟用 mbstring 擴(kuò)展庫(kù),在 php.ini里將; extension=php_mbstring.dll 前面的 ; 去掉
string iconv ( string in_charset, string out_charset, string str )
注意:
第二個(gè)參數(shù),除了可以指定要轉(zhuǎn)化到的編碼以外,還可以增加兩個(gè)后綴://TRANSLIT 和 //IGNORE,
其中:
//TRANSLIT 會(huì)自動(dòng)將不能直接轉(zhuǎn)化的字符變成一個(gè)或多個(gè)近似的字符,
//IGNORE 會(huì)忽略掉不能轉(zhuǎn)化的字符,而默認(rèn)效果是從第一個(gè)非法字符截?cái)唷?
Returns the converted string or FALSE on failure.
使用:
1. 發(fā)現(xiàn)iconv在轉(zhuǎn)換字符"-"到gb2312時(shí)會(huì)出錯(cuò),如果沒(méi)有ignore參數(shù),所有該字符后面的字符串都無(wú)法被保存。不管怎么樣,這
個(gè)"-"都無(wú)法轉(zhuǎn)換成功,無(wú)法輸出。另外mb_convert_encoding沒(méi)有這個(gè)bug.
2. mb_convert_encoding 可以指定多種輸入編碼,它會(huì)根據(jù)內(nèi)容自動(dòng)識(shí)別,但是執(zhí)行效率比iconv差太多;如:$str =
mb_convert_encoding($str,"euc-jp","ASCII,JIS,EUC-JP,SJIS,UTF- 8");“ASCII,JIS,EUC-JP,SJIS,UTF-8”的順序不同效果也有
差異
3. 一般情況下用 iconv,只有當(dāng)遇到無(wú)法確定原編碼是何種編碼,或者iconv轉(zhuǎn)化后無(wú)法正常顯示時(shí)才用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.
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");
$str = mb_convert_encoding($str, "EUC-JP', "auto");
例子:
$content = iconv("GBK", "UTF-8", $content);
$content = mb_convert_encoding($content, "UTF-8", "GBK");
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] )
需要先啟用 mbstring 擴(kuò)展庫(kù),在 php.ini里將; extension=php_mbstring.dll 前面的 ; 去掉
string iconv ( string in_charset, string out_charset, string str )
注意:
第二個(gè)參數(shù),除了可以指定要轉(zhuǎn)化到的編碼以外,還可以增加兩個(gè)后綴://TRANSLIT 和 //IGNORE,
其中:
//TRANSLIT 會(huì)自動(dòng)將不能直接轉(zhuǎn)化的字符變成一個(gè)或多個(gè)近似的字符,
//IGNORE 會(huì)忽略掉不能轉(zhuǎn)化的字符,而默認(rèn)效果是從第一個(gè)非法字符截?cái)唷?
Returns the converted string or FALSE on failure.
使用:
1. 發(fā)現(xiàn)iconv在轉(zhuǎn)換字符"-"到gb2312時(shí)會(huì)出錯(cuò),如果沒(méi)有ignore參數(shù),所有該字符后面的字符串都無(wú)法被保存。不管怎么樣,這
個(gè)"-"都無(wú)法轉(zhuǎn)換成功,無(wú)法輸出。另外mb_convert_encoding沒(méi)有這個(gè)bug.
2. mb_convert_encoding 可以指定多種輸入編碼,它會(huì)根據(jù)內(nèi)容自動(dòng)識(shí)別,但是執(zhí)行效率比iconv差太多;如:$str =
mb_convert_encoding($str,"euc-jp","ASCII,JIS,EUC-JP,SJIS,UTF- 8");“ASCII,JIS,EUC-JP,SJIS,UTF-8”的順序不同效果也有
差異
3. 一般情況下用 iconv,只有當(dāng)遇到無(wú)法確定原編碼是何種編碼,或者iconv轉(zhuǎn)化后無(wú)法正常顯示時(shí)才用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.
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");
$str = mb_convert_encoding($str, "EUC-JP', "auto");
例子:
$content = iconv("GBK", "UTF-8", $content);
$content = mb_convert_encoding($content, "UTF-8", "GBK");
相關(guān)文章
CentOS7系統(tǒng)搭建LAMP及更新PHP版本操作詳解
這篇文章主要介紹了CentOS7系統(tǒng)搭建LAMP及更新PHP版本操作,總結(jié)分析了CentOS7系統(tǒng)搭建LAMP及更新PHP版本操作相關(guān)原理、步驟、操作命令與注意事項(xiàng),需要的朋友可以參考下2020-03-03
基于wordpress主題制作的具體實(shí)現(xiàn)步驟
本篇文章是對(duì)wordpress主題制作的具體實(shí)現(xiàn)步驟進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
PHP簡(jiǎn)單獲取隨機(jī)數(shù)的常用方法小結(jié)
這篇文章主要介紹了PHP簡(jiǎn)單獲取隨機(jī)數(shù)的常用方法,結(jié)合實(shí)例形式分析了php實(shí)現(xiàn)指定范圍隨機(jī)數(shù)與指定字符序列隨機(jī)數(shù)的簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
完美解決phpexcel導(dǎo)出到xls文件出現(xiàn)亂碼的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決phpexcel導(dǎo)出到xls文件出現(xiàn)亂碼的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
php圖片處理函數(shù)獲取類型及擴(kuò)展名實(shí)例
這篇文章主要介紹了php圖片處理函數(shù)獲取類型及擴(kuò)展名的方法,包括image2wbmp、image_type_to_extension、image_type_to_mime_type等函數(shù)的具體使用,具有不錯(cuò)的借鑒與學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-11-11

