编码转换实验

来源:互联网 发布:网上兼职赚钱软件 编辑:程序博客网 时间:2024/05/21 07:46


一、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

原创粉丝点击