php字串編碼轉換樣本

來源:互聯網
上載者:User
  1. header("content-type: text/html; charset=utf-8");
  2. echo mb_convert_encoding("你是我的好朋友","utf-8","gbk");
  3. ?>
複製代碼

2、把 utf-8 編碼字串轉換成 gb2312 編碼字串

  1. // 注意將此檔案存檔成 utf-8 編碼格式檔案再測試
  2. header("content-type:text/html;charset=gb2312");
  3. echo mb_convert_encoding("你是我的好朋友","gb2312","utf-8");
  4. ?>
  5. // 注意將此檔案存檔成 utf-8 編碼格式檔案再測試
  6. header("content-type: text/html; charset=gb2312");
  7. echo mb_convert_encoding("你是我的好朋友","gb312","utf-8");
  8. ?>
複製代碼

3、對整個頁面進行轉換該方法適用所有編碼環境。這樣把前128個字元以外(顯示字元)的字元集都用 ncr(numeric character reference,如“漢字”將轉換成“汉字”這種形式)來表示,這樣的編碼在任意編碼環境下頁面都能正 常顯示。在php檔案的頭部加上下面三行代碼:

  1. mb_internal_encoding(”gb2312″); //gb2312 網站原來的編碼
  2. mb_http_output(”html-entities”);
  3. ob_start(’mb_output_handler’);
複製代碼

mb_internal_encoding(”gb2312″); // gb2312 網站原來的編碼 mb_http_output(”html-entities”); ob_start(’mb_output_handler’);使用mb_convert_encoding 函數需啟用php 的mbstring (multi-byte string)擴充。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.