php iconv() : Detected an illegal character in input string

來源:互聯網
上載者:User
開始是這樣用的
$str = iconv('UTF-8', 'GB2312', unescape(isset($_GET['str'])? $_GET['str']:''));
上線後報一堆這樣的錯:iconv() : Detected an illegal character in input string

考慮到GB2312字元集比較小,換個大的吧,於是改成GBK:
$str = iconv('UTF-8', 'GBK', unescape(isset($_GET['str'])? $_GET['str']:''));
上線後還是報同樣的錯!

再認真讀手冊,發現有這麼一段:
If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character.
於是改成:
$str = iconv('UTF-8', 'GBK//IGNORE', unescape(isset($_GET['str'])? $_GET['str']:''));
本地測試//IGNORE能忽略掉它不認識的字接著往下轉,並且不報錯,而//TRANSLIT是截掉它不認識的字及其後面的內容,並且報錯。//IGNORE是我需要的。
現在等待上線看結果(這樣不是好的做法,繼續琢磨手冊,上網搜搜看),呵呵。。。

在網上找到下面這篇文章,發現mb_convert_encoding也可以,但效率比iconv差。


轉換字串編碼iconv與mb_convert_encoding的區別

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 擴充庫,在 php.ini裡將; extension=php_mbstring.dll 前面的 ; 去掉

string iconv ( string in_charset, string out_charset, string str )
注意:
第二個參數,除了可以指定要轉化到的編碼以外,還可以增加兩個尾碼://TRANSLIT 和 //IGNORE,
其中:
//TRANSLIT 會自動將不能直接轉化的字元變成一個或多個近似的字元,
//IGNORE 會忽略掉不能轉化的字元,而預設效果是從第一個非法字元截斷。
Returns the converted string or FALSE on failure.

使用:
1. 發現iconv在轉換字元"-"到gb2312時會出錯,如果沒有ignore參數,所有該字元後面的字串都無法被儲存。不管怎麼樣,這個"-"都無法轉換成功,無法輸出。另外mb_convert_encoding沒有這個bug.
2. mb_convert_encoding 可以指定多種輸入編碼,它會根據內容自動識別,但是執行效率比iconv差太多;如:$str = mb_convert_encoding($str,"euc-jp","ASCII,JIS,EUC-JP,SJIS,UTF- 8");“ASCII,JIS,EUC-JP,SJIS,UTF-8”的順序不同效果也有差異
3. 一般情況下用 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.

$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");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.