今天在提取抓取到的网页内容的正文时候,发现结果老是不完整,开始以为是提取不正确,然后去一步步分析提取结果,发现没问题,最后才发现是编码转换的环节的问题。我开始是直接使用iconv函数, $txtContent = iconv("utf-8",'GBK',$txtContent); utf-8直接转gbk,这样问题就来了,当有些字符无法转换的时候就从此处断开,导致内容不完整。后来又重新查手册,才发现iconv还有两个可选的辅助参数:TRANSLIT和IGNORE ,(其中IGNORE 就是说遇到无法转换的就跳过)。 其实也怪自己太粗心,刚开始一看iconv函数参数就三个,而且例子中也没有特别的,就懒得去看英文解释了,直接使用,直到出现了问题。 Descriptionstring 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. |
原文链接:http://hi.baidu.com/yukinzhang/blog/item/82ad8e811baaf3debc3e1ede.html