再認真讀手冊,發現有這麼一段:
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)
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.