xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····
今天在提取抓取到的网页内容的正文时候,发现结果老是不完整,开始以为是提取不正确,然后去一步步分析提取结果,发现没问题,最后才发现是编码转换的环节的问题。我开始是直接使用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 小果子 阅读(208) 评论(0)  编辑 收藏 引用 所属分类: 学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理