Table of Contents
PHP中iconv函数知识汇总
Home php教程 php手册 PHP中iconv函数知识汇总

PHP中iconv函数知识汇总

Jun 13, 2016 am 08:59 AM
iconv php function character set Finish Library Summary Knowledge were able

PHP中iconv函数知识汇总

  iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。本文内容是参考了网上的其他资源,然后结合自己的实践,有需要的小伙伴可以参考下。

  今天在修改论文在线的时候,遇到了iconv这个函数。学习一下

  ?

  1

  2

  3

  4header('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的名字。

  下边是关于这个函数的详细和延伸用法

  ?

  1iconv("UTF-8","GB2312//IGNORE",$data)

  ignore的意思是忽略转换时的错误,如果没有ignore参数,所有该字符后面的字符串都无法被保存。

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

  例子:

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  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

  安装:

  ?

  1

  2

  3

  4

  5#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/

  使用的简单例子:

  ?

  1

  2

  3

  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

  ?

  1

  2

  3

  4

  header("content-Type: text/html; charset=Utf-8");

  echo mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK");

  ?>

  再来个GB2312 To Big5

  ?

  1

  2

  3

  4

  header("content-Type: text/html; charset=big5");

  echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");

  ?>

  不过要使用上面的函数需要安装但是需要先enable mbstring 扩展库。

  PHP中的另外一个函数iconv也是用来转换字符串编码的,与上函数功能相似。

  下面还有一些详细的例子:

  ?

  1

  2

  3

  4iconv — 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”);

  例子:

  ?

  1

  2$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 就是说遇到无法转换的就跳过)。

  以上所述就是本文的全部内容了,希望大家能够喜欢。

 

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles