一、utf-8 转换为 gb2312
〈?php
//header(“Content-Type:text/html; charset=utf-8“);
?〉
〈html xmlns=“http://www.w3.org/1999/xhtml“〉
〈head〉
〈meta http-equiv=“Content-Type“ content=“text/html; charset=utf-8“〉
〈/head〉
〈body〉
本页面编码是:utf-8 BOM〈br〉
〈?php
$str = ’陶喆’;
echo ’utf-8:’. $str;
echo ’〈br〉’;
$str = iconv(’utf-8’, ’gb2312//IGNORE’, $str); //convert to gb2312
//这是iconv函数的一个bug。iconv在转换字符到gb2312时会出错。
//解决方法很简单,就是在需要转成的编码后加 “//IGNORE“。
echo ’gb2312:’. $str;
echo ’〈br〉’;
$str = iconv(’gb2312’, ’utf-8’, $str); //再还原回来
//“喆“字没有了,是因为gb2312编码里没有这个字
//本身语法是正确的。变成 gbk 编码就可以了
echo ’utf-8:’. $str;
?〉
二、utf-8 转换为 gbk
〈?php
//header(“Content-Type:text/html; charset=utf-8“);
?〉
〈html xmlns=“http://www.w3.org/1999/xhtml“〉
〈head〉
〈meta http-equiv=“Content-Type“ content=“text/html; charset=utf-8“〉
〈/head〉
〈body〉
本页面编码是:utf-8 BOM〈br〉
〈?php
$str = ’陶喆’;
echo ’utf-8:’. $str;
echo ’〈br〉’;
$str = iconv(’utf-8’, ’gbk’, $str); //convert to gbk
echo ’gb2312:’. $str;
echo ’〈br〉’;
$str = iconv(’gbk’, ’utf-8’, $str); //再还原回来
echo ’utf-8:’. $str;
?〉
〈/body〉
[文章来源:“十万个为什么”电脑学习网]
[网络地址:http://why100000.com]
本文来自“十万个为什么”电脑学习网 http://www.why100000.com
本文介绍如何使用PHP的iconv函数进行utf-8到gb2312及gbk的字符集转换,并解释了在转换过程中可能遇到的问题及其解决方案。
3778

被折叠的 条评论
为什么被折叠?



