php iconv() : Detected an illegal character in input string
更新時間:2010年12月05日 16:44:19 作者:
PHP傳給JS字符串用ecsape轉(zhuǎn)換加到url里,又用PHP接收,再用網(wǎng)上找的unscape函數(shù)轉(zhuǎn)換一下,這樣得到的字符串是UTF-8的,但我需要的是GB2312,于是用iconv轉(zhuǎn)換
開始是這樣用的
$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']:''));
上線后還是報同樣的錯!
再認(rèn)真讀手冊,發(fā)現(xiàn)有這么一段:
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能忽略掉它不認(rèn)識的字接著往下轉(zhuǎn),并且不報錯,而//TRANSLIT是截掉它不認(rèn)識的字及其后面的內(nèi)容,并且報錯。//IGNORE是我需要的。
現(xiàn)在等待上線看結(jié)果(這樣不是好的做法,繼續(xù)琢磨手冊,上網(wǎng)搜搜看),呵呵。。。
在網(wǎng)上找到下面這篇文章,發(fā)現(xiàn)mb_convert_encoding也可以,但效率比iconv差。
轉(zhuǎn)換字符串編碼iconv與mb_convert_encoding的區(qū)別
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 )
注意:
第二個參數(shù),除了可以指定要轉(zhuǎn)化到的編碼以外,還可以增加兩個后綴://TRANSLIT 和 //IGNORE,
其中:
//TRANSLIT 會自動將不能直接轉(zhuǎn)化的字符變成一個或多個近似的字符,
//IGNORE 會忽略掉不能轉(zhuǎn)化的字符,而默認(rèn)效果是從第一個非法字符截斷。
Returns the converted string or FALSE on failure.
使用:
1. 發(fā)現(xiàn)iconv在轉(zhuǎn)換字符"-"到gb2312時會出錯,如果沒有ignore參數(shù),所有該字符后面的字符串都無法被保存。不管怎么樣,這個"-"都無法轉(zhuǎn)換成功,無法輸出。另外mb_convert_encoding沒有這個bug.
2. mb_convert_encoding 可以指定多種輸入編碼,它會根據(jù)內(nèi)容自動識別,但是執(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)遇到無法確定原編碼是何種編碼,或者iconv轉(zhuǎn)化后無法正常顯示時才用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");
$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']:''));
上線后還是報同樣的錯!
再認(rèn)真讀手冊,發(fā)現(xiàn)有這么一段:
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能忽略掉它不認(rèn)識的字接著往下轉(zhuǎn),并且不報錯,而//TRANSLIT是截掉它不認(rèn)識的字及其后面的內(nèi)容,并且報錯。//IGNORE是我需要的。
現(xiàn)在等待上線看結(jié)果(這樣不是好的做法,繼續(xù)琢磨手冊,上網(wǎng)搜搜看),呵呵。。。
在網(wǎng)上找到下面這篇文章,發(fā)現(xiàn)mb_convert_encoding也可以,但效率比iconv差。
轉(zhuǎn)換字符串編碼iconv與mb_convert_encoding的區(qū)別
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 )
注意:
第二個參數(shù),除了可以指定要轉(zhuǎn)化到的編碼以外,還可以增加兩個后綴://TRANSLIT 和 //IGNORE,
其中:
//TRANSLIT 會自動將不能直接轉(zhuǎn)化的字符變成一個或多個近似的字符,
//IGNORE 會忽略掉不能轉(zhuǎn)化的字符,而默認(rèn)效果是從第一個非法字符截斷。
Returns the converted string or FALSE on failure.
使用:
1. 發(fā)現(xiàn)iconv在轉(zhuǎn)換字符"-"到gb2312時會出錯,如果沒有ignore參數(shù),所有該字符后面的字符串都無法被保存。不管怎么樣,這個"-"都無法轉(zhuǎn)換成功,無法輸出。另外mb_convert_encoding沒有這個bug.
2. mb_convert_encoding 可以指定多種輸入編碼,它會根據(jù)內(nèi)容自動識別,但是執(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)遇到無法確定原編碼是何種編碼,或者iconv轉(zhuǎn)化后無法正常顯示時才用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");
您可能感興趣的文章:
- 問個高難度的復(fù)雜查詢(在一個時間段內(nèi)的間隔查詢)
- PHP syntax error, unexpected $end 錯誤的一種原因及解決
- jQuery選中select控件 無法設(shè)置selected的解決方法
- php異常:Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE eval()''d code error
- PHP Parse Error: syntax error, unexpected $end 錯誤的解決辦法
- w3wp進程發(fā)生死鎖ISAPI aspnet_isapi.dll報告它自身有問題,原因Deadlock detected
- jquery.bgiframe.js在IE9下提示INVALID_CHARACTER_ERR錯誤
- C++中的三種繼承public,protected,private詳細(xì)解析
- CodeIgniter框架提示Disallowed Key Characters的解決辦法
- PHP異常Parse error: syntax error, unexpected T_VAR錯誤解決方法
- pycharm 使用心得(九)解決No Python interpreter selected的問題
- PHP錯誤Parse error: syntax error, unexpected end of file in test.php on line 12解決方法
- 強制SQL Server執(zhí)行計劃使用并行提升在復(fù)雜查詢語句下的性能
- 深入理解C++中public、protected及private用法
- jQuery中:selected選擇器用法實例
- 在sqlserver中如何使用CTE解決復(fù)雜查詢問題
相關(guān)文章
PHP中通過ADODB庫實現(xiàn)調(diào)用Access數(shù)據(jù)庫之修正版本
PHP中通過ADODB庫實現(xiàn)調(diào)用Access數(shù)據(jù)庫之修正版本...2006-12-12php+resumablejs實現(xiàn)的分塊上傳 斷點續(xù)傳功能示例
這篇文章主要介紹了php+resumablejs實現(xiàn)的分塊上傳 斷點續(xù)傳功能,結(jié)合實例形式分析了php+resumablejs文件傳輸?shù)木唧w實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04ThinkPHP使用PHPExcel實現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出完整實例
這篇文章主要介紹了ThinkPHP使用PHPExcel實現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出,非常實用的功能,需要的朋友可以參考下2014-07-07