今天在提取抓取到的網頁內容的正文時候,發現結果老是不完整,開始以為是提取不正確,然后去一步步分析提取結果,發現沒問題,最后才發現是編碼轉換的環節的問題。我開始是直接使用iconv函數,
$txtContent = iconv("utf-8",'GBK',$txtContent);
utf-8直接轉gbk,這樣問題就來了,當有些字符無法轉換的時候就從此處斷開,導致內容不完整。后來又重新查手冊,才發現iconv還有兩個可選的輔助參數:TRANSLIT和IGNORE ,(其中IGNORE 就是說遇到無法轉換的就跳過)。 其實也怪自己太粗心,剛開始一看iconv函數參數就三個,而且例子中也沒有特別的,就懶得去看英文解釋了,直接使用,直到出現了問題。
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
小果子 閱讀(225)
評論(0) 編輯 收藏 引用 所屬分類:
學習筆記