关于iconv容易忽略的参数IGNORE

来源:互联网 发布:centos 7安装桌面环境 编辑:程序博客网 时间:2024/05/21 12:07
 今天在提取抓取到的网页内容的正文时候,发现结果老是不完整,开始以为是提取不正确,然后去一步步分析提取结果,发现没问题,最后才发现是编码转换的环节的问题。我开始是直接使用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 fromin_charset to out_charset. Returns the converted string orFALSE on failure.

If you append the string //TRANSLIT toout_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

原创粉丝点击