php中iconv函数使用问题

问题:最近在做一个程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些。 iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。 1、下载libiconv函数库http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.9.2.tar.gz; 2、解压缩tar -zxvf libiconv-1.9.2.tar.gz; 3、安装libiconv #configure --prefix=/usr/local/iconv #make #make install 4、重新编译php 增加编译参数--with-iconv=/usr/local/iconv windows下 最 近在做一个小偷程序,需要用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些。 让我郁闷了好一会儿,去网上一查资料才知道这是iconv函数的一个bug。iconv在转换字符"—"到gb2312时会出错 解决方法很简单,就是在需要转成的编码后加 "//IGNORE" 也就是iconv函数第二个参数后.如下: 以下为引用的内容:
复制代码 代码如下:
iconv("UTF-8","GB2312//IGNORE",$data)
ignore的意思是忽略转换时的错误,如果没有ignore参数,所有该字符后面的字符串都无法被保存。
复制代码 代码如下:
<?php echo $str= '你好,这里是卖咖啡!'; echo '<br />'; echo iconv('GB2312', 'UTF-8', $str); //将字符串的编码从GB2312转到UTF-8 echo '<br />'; echo iconv_substr($str, 1, 1, 'UTF-8'); //按字符个数截取而非字节 print_r(iconv_get_encoding()); //得到当前页面编码信息 echo iconv_strlen($str, 'UTF-8'); //得到设定编码的字符串长度 //也有这样用的 $content = iconv("UTF-8","gbk//TRANSLIT",$content); ?>
iconv不是php的默认函数,也是默认安装的模块。需要安装才能用的。 如 果是windows2000+php,你可以修改php.ini文件,将extension=php_iconv.dll前的";"去掉,同时你要 copy你的原php安装文件下的iconv.dll到你的winnt/system32下(如果你的dll指向的是这个目录) 在linux环境下,用静态安装的方式,在configure时加多一项 --with-iconv就可以了,phpinfo看得到iconv的项。(Linux7.3+Apache4.06+php4.3.2), 下载:ftp://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.8.tar.gz 安装: #cp libiconv-1.8.tar.gz /usr/local/src #tar zxvf lib* #./configure --prefix=/usr/local/libiconv #make #make install 编译php #./configure --prefix=/usr/local/php4.3.2 --with-iconv=/usr/local/libiconv/ 使用的简单例子: <?php echo iconv("gb2312","ISO-8859-1","我们"); ?> PHP中的mb_convert_encoding与iconv函数介绍 mb_convert_encoding这个函数是用来转换编码的。原来一直对程序编码这一概念不理解,不过现在好像有点开窍了。 不过英文一般不会存在编码问题,只有中文数据才会有这个问题。比如你用Zend Studio或Editplus写程序时,用的是gbk编码,如果数据需要入数据库,而数据库的编码为utf8时,这时就要把数据进行编码转换,不然进到数据库就会变成乱码。 mb_convert_encoding的用法见官方: http://cn.php.net/manual/zh/function.mb-convert-encoding.php 做一个GBK To UTF-8 < ?php header("content-Type: text/html; charset=Utf-8"); echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK"); ?> 再来个GB2312 To Big5 < ?php header("content-Type: text/html; charset=big5"); echo mb_convert_encoding("你是我的朋友", "big5", "GB2312"); ?> 不过要使用上面的函数需要安装但是需要先enable mbstring 扩展库。 PHP中的另外一个函数iconv也是用来转换字符串编码的,与上函数功能相似。 下面还有一些详细的例子: iconv — Convert string to requested character encoding (PHP 4 >= 4.0.5, PHP 5) mb_convert_encoding — Convert character encoding (PHP 4 >= 4.0.6, PHP 5) 用法: string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] ) 需要先enable mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉 mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多; string iconv ( string in_charset, string out_charset, string str ) 注 意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。 Returns the converted string or FALSE on failure. 使用: 发现iconv在转换字符”—”到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个”—”都无法转换成功,无法输出。 另外mb_convert_encoding没有这个bug. 一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数. from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. If it is not specified, the internal encoding will be used. /* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */ $str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”); /* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */ $str = mb_convert_encoding($str, “EUC-JP”, “auto”); 例子: $content = iconv(”GBK”, “UTF-8″, $content); $content = mb_convert_encoding($content, "UTF-8″,"GBK"); php中使用iconv函数时容易忽略的参数 今 天在处理抓取内容的时候,当采用iconv进行编码转换的时候,发现结果会中断,猜是字符集的问题,考虑怎么跳过目标字符集不存在的字符,查手册发现 iconv的函数只有三个参数,好像不行,然后查网上有人说可以,但是很奇怪怎么实现,最后发现英文描述有说可以加标识到目标编码后 面:“TRANSLIT”,很郁闷怎么加呢?原来是先加“//”,真是郁闷,竟然有这样的设计 原型: $txtContent = iconv("utf-8",'GBK',$txtContent); 特殊参数:iconv("UTF-8","GB2312//IGNORE",$data) 两个可选的辅助参数:TRANSLIT和IGNORE ,(其中IGNORE 就是说遇到无法转换的就跳过)。 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.
原文地址:https://www.cnblogs.com/gxldan/p/4066868.html