今天在提取抓取到的網(wǎng)頁內容的正文時候,發(fā)現(xiàn)結果老是不完整,開始以為是提取不正確,然后去一步步分析提取結果,發(fā)現(xiàn)沒問題,最后才發(fā)現(xiàn)是編碼轉換的環(huán)節(jié)的問題。我開始是直接使用iconv函數(shù),
$txtContent = iconv("utf-8",'GBK',$txtContent);
utf-8直接轉gbk,這樣問題就來了,當有些字符無法轉換的時候就從此處斷開,導致內容不完整。后來又重新查手冊,才發(fā)現(xiàn)iconv還有兩個可選的輔助參數(shù):TRANSLIT和IGNORE ,(其中IGNORE 就是說遇到無法轉換的就跳過)。 其實也怪自己太粗心,剛開始一看iconv函數(shù)參數(shù)就三個,而且例子中也沒有特別的,就懶得去看英文解釋了,直接使用,直到出現(xiàn)了問題。
Description
string iconv ( string in_charset, string out_charset, string str )
Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.
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.
posted on 2012-09-03 15:46
小果子 閱讀(227)
評論(0) 編輯 收藏 引用 所屬分類:
學習筆記