php中的iconv functions

原创 2004年08月19日 20:56:00

介绍

This module contains an interface to the iconv library functions. The iconv library functions convert strings between various character sets encodings. The supported character sets depend on the iconv() implementation on your system. Note that the iconv() function on some systems may not work as well as you expect. In this case, you should install the libiconv library.

需求

Your systems standard C library must provide the iconv() function or you must have libiconv installed on your system. The libiconv library is available from http://www.gnu.org/software/libiconv/.

安装

To be able to use the functions defined in this module you must compile your PHP interpreter using the configure line --with-iconv[=DIR].

Note to Win32 Users: In order to enable this module on a Windows environment, you must copy iconv-1.3.dll from the DLL folder of the PHP/Win32 binary package to the SYSTEM32 folder of your windows machine. (Ex: C:/WINNT/SYSTEM32 or C:/WINDOWS/SYSTEM32). Starting with PHP 4.2.1 the name changed to iconv.dll

运行时配置

这些函数的行为受到全局配置文件 php.ini 的影响。

表格 1. Iconv configuration options

Name Default Changeable
iconv.input_encoding ICONV_INPUT_ENCODING PHP_INI_ALL
iconv.output_encoding ICONV_OUTPUT_ENCODING PHP_INI_ALL
iconv.internal_encoding ICONV_INTERNAL_ENCODING PHP_INI_ALL
For further details and definition of the PHP_INI_* constants see ini_set().

资源类型

该扩展模块未定义任何资源类型。

预定义常量

Since PHP 4.3.0 it is possible to identify at runtime which iconv implementation is adopted by this extension.

表格 2. iconv constants

constant type description
ICONV_IMPL string The implementation name
ICONV_VERSION string The implementation version

注: Writing implementation-dependent scripts with these constants should be discouraged.


iconv_get_encoding

(PHP 4 >= 4.0.5)

iconv_get_encoding -- Get current setting for character encoding conversion

Description

array iconv_get_encoding ( [string type])

It returns the current settings of ob_iconv_handler() as array or FALSE on failure. The value of the optinal type can be:

all
input_encoding
output_encoding
internal_encoding

If type is omitted or not 'all' iconv_get_encoding() returns the current settings of ob_iconv_handler() as string.

例子 1. iconv_get_encoding() example:

<pre>
<?php
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "ISO-8859-1");
var_dump(iconv_get_encoding('all'));
?>
</pre>

The printout of the above program will be:

Array

(

    [input_encoding] => ISO-8859-1

    [output_encoding] => ISO-8859-1

    [internal_encoding] => UTF-8

)

iconv_set_encoding

(PHP 4 >= 4.0.5)

iconv_set_encoding -- Set current setting for character encoding conversion

Description

bool iconv_set_encoding ( string type, string charset)

It changes the value of type to charset. 如果成功则返回 TRUE,失败则返回 FALSE

The value of type can be:

input_encoding
output_encoding
internal_encoding

例子 1. iconv_set_encoding() example:

iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "ISO-8859-1");

iconv

(PHP 4 >= 4.0.5)

iconv -- Convert string to requested character encoding

Description

string iconv ( string in_charset, string out_charset, string str)

It converts the string str encoded in in_charset to the string encoded in out_charset. It returns the converted string or FALSE, if it fails.

例子 1. iconv() example:

echo iconv("ISO-8859-1","UTF-8","This is a test.");

ob_iconv_handler

(PHP 4 >= 4.0.5)

ob_iconv_handler -- Convert character encoding as output buffer handler

Description

array ob_iconv_handler ( string contents, int status)

It converts the string encoded in internal_encoding to output_encoding.

internal_encoding and output_encoding should be defined by iconv_set_encoding() or in the configuration file php.ini.

例子 1. ob_iconv_handler() example:

ob_start("ob_iconv_handler"); // start output buffering

php中 iconv 函数使用方法

转载自:http://apps.hi.baidu.com/share/detail/1797448 unix下安装PHP的module,需要重新编译PHP,Windows下安装模板,只需将php.i...
  • liruxing1715
  • liruxing1715
  • 2011-12-09 11:43:15
  • 4146

php中iconv函数使用方法,解决中文乱码

iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。  1、下载libiconv函数库libiconv-1.x.tar.gz;  2、解压缩tar -zxvf ...
  • zhx1259135417
  • zhx1259135417
  • 2014-11-25 17:35:34
  • 7366

PHP iconv()字符编码转换的问题

在php函数库有一个函数:iconv(),iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。最近在做一个小偷程序,需要用到iconv函数把抓取来过的utf-8编码的页面转...
  • tashanhongye
  • tashanhongye
  • 2015-12-17 20:47:28
  • 2124

Windows下PHP安装iconv【矫正版】

以前遇到这个问题,在网上查的时候还比较容易搞定,这次,看到一个版本的N多转载,下载连接都是错误的。一点用处都没有。先把两份比较有用的资料整理。 Windows+Apache下PHP安装iconvNix...
  • bitzi
  • bitzi
  • 2008-07-28 15:13:00
  • 1160

PHP iconv 解决utf-8和gb2312编码转换问题

就一个很简单的函数iconv();但是就是这个函数在网上找了很多例子,都无法成功转换,这是为什么呢? 终于皇天不负有心人,答案还是让我找到了。  网上的都是这样用的,代码如下:...
  • qq_15796423
  • qq_15796423
  • 2016-07-07 16:44:45
  • 2768

编译php5遇到iconv错误的解决方法

第一个方法:在make 时,用make ZEND_EXTRA_LIBS='-liconv'代替第二方法,麻烦一些http://rental.zhupiter.com/postshow_273_1_1....
  • guichenglin
  • guichenglin
  • 2011-03-16 17:35:00
  • 1384

php转码中的iconv与mb_convert_encoding的理解

由于php内部未采用unicode编码,因此php在处理高阶字符。如中文,日文等时,均有乱码的情况。解决方案就是转码,php有两个主要的转码函数。iconv和mb_convert_encoding。 ...
  • frycn
  • frycn
  • 2017-04-19 17:03:43
  • 2800

php iconv转编码 数据丢失的解决方法

今天在处理抓取内容的时候,当采用iconv进行编码转换的时候,发现结果会中断,猜是字符集的问题,考虑怎么跳过目标字符集不存在的字符,查手册发现iconv的函数只有三个参数,好像不行,然后查网上有人说可...
  • qqahanson
  • qqahanson
  • 2015-02-17 10:18:11
  • 1563

PHP使用iconv函数遍历数组转换字符集

/** * 字符串/二维数组/多维数组编码转换 * @param string $in_charset * @param string $out_charset * @param mixed $d...
  • moqiang02
  • moqiang02
  • 2014-04-02 21:19:50
  • 1571

php处理多字节字符mbstring及编码转换iconv

由于php内部使用单字节形式存储数据。这使得php在处理多字节文字的时候先天欠缺。只要是对数据进行原样处理。不进行计数,截取等。一般问题也不大,不幸的是对中文等多字节字符的截取,计数是一个很常见的问题...
  • frycn
  • frycn
  • 2017-04-11 16:57:35
  • 790
收藏助手
不良信息举报
您举报文章:php中的iconv functions
举报原因:
原因补充:

(最多只允许输入30个字)