国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

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

2024-05-04 23:05:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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']:''));
上線后報(bào)一堆這樣的錯(cuò):iconv() : Detected an illegal character in input string

考慮到GB2312字符集比較小,換個(gè)大的吧,于是改成GBK:
$str = iconv('UTF-8', 'GBK', unescape(isset($_GET['str'])? $_GET['str']:''));
上線后還是報(bào)同樣的錯(cuò)!

再認(rèn)真讀手冊(cè),發(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']:''));
本地測(cè)試//IGNORE能忽略掉它不認(rèn)識(shí)的字接著往下轉(zhuǎn),并且不報(bào)錯(cuò),而//TRANSLIT是截掉它不認(rèn)識(shí)的字及其后面的內(nèi)容,并且報(bào)錯(cuò)。//IGNORE是我需要的。
現(xiàn)在等待上線看結(jié)果(這樣不是好的做法,繼續(xù)琢磨手冊(cè),上網(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 擴(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)唷?br />Returns the converted string or FALSE on failure.

使用:
1. 發(fā)現(xiàn)iconv在轉(zhuǎn)換字符"-"到gb2312時(shí)會(huì)出錯(cuò),如果沒有ignore參數(shù),所有該字符后面的字符串都無(wú)法被保存。不管怎么樣,這個(gè)"-"都無(wú)法轉(zhuǎn)換成功,無(wú)法輸出。另外mb_convert_encoding沒有這個(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");

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东乡| 大城县| 海口市| 潞西市| 砀山县| 肇源县| 龙泉市| 孟津县| 亚东县| 新密市| 广东省| 五莲县| 高碑店市| 宜兴市| 康马县| 宝清县| 宁海县| 故城县| 胶州市| 子洲县| 苍溪县| 政和县| 界首市| 新化县| 龙陵县| 鄂温| 青州市| 儋州市| 蕉岭县| 英山县| 福鼎市| 鄂托克旗| 大兴区| 盐边县| 武平县| 西和县| 云和县| 衡水市| 九寨沟县| 隆昌县| 驻马店市|