Home  >  Article  >  Backend Development  >  PHPiconv函数,已知函数fx_PHP教程

PHPiconv函数,已知函数fx_PHP教程

WBOY
WBOYOriginal
2016-07-13 09:52:131063browse

PHPiconv函数,已知函数fx

今天在修改论文在线的时候,遇到了iconv这个函数。学习一下
header('Content-Type: application/vnd.ms-excel;charset=UTF-8"');
$name=iconv('utf-8', 'gb2312', $data['year'].'年,第'.$data['period'].'期通信录');
header('Content-Disposition: attachment;filename="' . $name . '.xls"');
header('Cache-Control: max-age=0');
这段代码的意思,就是把utf-8格式转化为gb2312的格式,然后赋值给$name,这样导出excel文件的名字的时候,就是中文的$name的名字。
下边是关于这个函数的详细和延伸用法




来源原网址(www.cnblogs.com/keheng/articles/2496017.html);
iconv("UTF-8","GB2312//IGNORE",$data)
ignore的意思是忽略转换时的错误,如果没有ignore参数,所有该字符后面的字符串都无法被保存。

这个iconv()这个函数,在php5中是内置的.谢谢.

例子:
  echo $str= '你好,这里是卖咖啡!';
 echo '
';
 echo iconv('GB2312', 'UTF-8', $str);      //将字符串的编码从GB2312转到UTF-8
 echo '
';
 
 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/
使用的简单例子:

  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

  header("content-Type: text/html; charset=Utf-8");
  echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");
?>

 

再来个GB2312 To Big5

 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 函数.

1 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.
2 /* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
3 $str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”);
4 /* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */
5 $str = mb_convert_encoding($str, “EUC-JP”, “auto”);

 

例子:

1 $content = iconv(”GBK”, “UTF-8″, $content);
2 $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.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1011003.htmlTechArticlePHPiconv函数,已知函数fx 今天在修改论文在线的时候,遇到了iconv这个函数。学习一下 header('Content-Type: application/vnd.ms-excel;charset=UTF-8"'); $n...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn