php中iconv函数利用要领

iconv函数库可以或许完成各类字符集间的转换,是php编程中不行缺少的基本函数库。
1、下载libiconv函数库;
2、解压缩tar -zxvf libiconv-1.9.2.tar.gz;
3、安装libiconv
       #configure --prefix=https://www.jb51.net/usr/local/iconv
       #make
       #make install
4、从头编译php 增加编译参数--with-iconv=https://www.jb51.net/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=https://www.jb51.net/usr/local/libiconv
#make
#make install
编译php
#./configure --prefix=https://www.jb51.net/usr/local/php4.3.2 --with-iconv=https://www.jb51.net/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的用法见官方:


做一个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.heiqu.com/wsjyxg.html