星期一, 4月 02, 2012

[PHP] iconv big5轉換utf8問題

雖然全世界都知道會有許功蓋問題,可是還是有不少網站在資料庫裡都還是採用big5碼
之前改到某知名公司所做的的ERP系統,用的是MS-SQL
因為查詢有點慢,所以想要做一個local cache來做一點暫存

用PHP的iconv算是很一般的做法,可是裡面有點問題
以下是測試的CODE:
header("Content-type: text/html; charset=utf-8");
$string = "轉碼測試功蓋銹abc轉碼測試";
$result = iconv("big5","UTF-8",$string);
echo $result;
印出的結果:
轉碼測試功蓋
明顯可以看到這不是許功蓋的錯
稍微查一下,照這個網站說的
"libiconv 不認得七個 BIG5 擴充字:碁, 銹, 裏(請改用'裡'), 墻, 恒, 粧, 嫺,無法將之轉換成對應的 UTF-8;但 Windows API 認得且可以轉換成 UTF-8。"

使用了
php中由"big5轉utf8,iconv不是個負責任的函數"的解法後,轉出的情形如下


轉碼測試功蓋 abc轉碼測試

雖然掉字問題獲得解決,可是也只是跳過而已,並沒有轉出
這時發現另外一個函式:mb_convert_encoding
好像就能解決這個問題,程式碼如下:

$string = "轉碼測試功蓋銹abc轉碼測試";
$result = mb_convert_encoding($string,"utf-8","big5");
echo $result;


mb_convert_encoding的速度好像會比iconv慢不少,不過對於這次的工作來說似乎沒有影響
所以,那又是另外一段故事了...

延伸閱讀:

http://twycf.com/wordpress/archives/155
http://www.ps3w.net/modules/psbb/?op=openthr&pos_id=1101

5 則留言:

陳木林 提到...

鄭小安 提到...

感謝

Ray 提到...

thanks

sabreenaaaberg 提到...

A full guide to the latest ford escape titanium 2021 - TheTianium
This page is a complete guide head titanium ti s6 for you to the all remmington titanium new titanium bike frame ford escape titanium 2021. With over 20 years of 토토 사이트 experience in the gambling industry, the titanium alloy nier company has

lousme 提到...

ue609 rieker 靴,rieker kabelky,rieker sko,klektgreece,rieker antwerpen,flight club sneakers,klektcanada,klektespana,klekt paris fv198