編碼轉換實驗


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

發佈了32 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章