php中字符集转换iconv函数使用总结,字符集转换iconv
php中字符集转换iconv函数使用总结,字符集转换iconv
iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。
用法如下:
复制代码 代码如下:
$string = "欢迎访问帮客之家!";
iconv("utf8","gbk",$string)//将字符串string 编码由utf8转变成gbk;
扩展如下:
复制代码 代码如下:
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);
备注:
1、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);
到此php中的iconv 介绍完毕
2、用iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些。原因是这样的:
复制代码 代码如下:
string iconv ( string in_charset, string out_charset, string str )
注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。 被截断了,当然就会少了;
可以这样修改iconv("UTF-8","GB2312//IGNORE",$string) 会忽略掉不能转化的字符;
附加:
PHP中的mb_convert_encoding与iconv函数介绍
mb_convert_encoding这个函数是用来转换编码的,和iconv函数差不多。
英文一般不会存在编码问题,只有中文数据才会有这个问题。比如你用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"); //设置字符的编码是utp-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.ini。
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差太多,那为何还要讲解 这个 mb_convert_encoding()函数呢?答案如下:
发现iconv在转换字符”—”到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个”—”都无法转换成功,无法输出。 另外mb_convert_encoding没有这个bug.
一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数.
保留下来也显示不了 别使用gbk格式了
iconv('gbk','utf-8',$tomename);
你这样写试试 我一直这么用的
utf-8后边的IGMORE是什么意思?问一下

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
