php轉換字串的字元編碼

2021-08-31 15:04:24 字數 1204 閱讀 5480

1、把 gbk 編碼字串轉換成 utf-8 編碼字串 view plaincopy to clipboardprint?

<?php  

header("content-type: text/html; charset=utf-8");  

echo mb_convert_encoding("你是我的好朋友","utf-8","gbk");  

?>  

2、把 utf-8 編碼字串轉換成 gb2312 編碼字串 view plaincopy to clipboardprint?

// 注意將此檔案存檔成   utf-8 編碼格式檔案再測試  

<?php  

header("content-type:text/html;charset=gb2312");  

echo mb_convert_encoding("你是我的好朋友","gb2312","utf-8");  

?>   

// 注意將此檔案存檔成   utf-8 編碼格式檔案再測試

<?php

header("content-type: text/html; charset=gb2312");

echo mb_convert_encoding("你是我的好朋友","gb312","utf-8");

?>

3、對整個頁面進行轉換

該方法適用所有編碼環境。這樣把前128個字元以外(顯示字元)的字符集都用   ncr(numeric character reference,如「漢字」將轉換成「漢字」這種形式)來表示,這樣的編碼在任意編碼環境下頁面都能正常顯示。

在php檔案的頭部加上下面三行**:

view plaincopy to clipboardprint?

mb_internal_encoding(」gb2312″);  // 這裡的gb2312是你**原來的編碼  

mb_internal_encoding(」gb2312″);   // 這裡的gb2312是你**原來的編碼 mb_http_output(」html-entities」); ob_start(』mb_output_handler』);

使用mb_convert_encoding 函式需啟用php 的mbstring (multi-byte string)擴充套件

php字串編碼轉換

使用舉例 1 把 gbk 編碼字串轉換成 utf 8 編碼字串 view plaincopy toclipboardprint?header content type text html charset utf 8 echomb convert encoding 你是我的好朋友 utf 8 gbk ...

java 字串編碼轉換

public class changecharset 將字元編碼轉換成iso 8859 1碼 public static string toiso 8859 1 string str throws unsupportedencodingexception 將字元編碼轉換成utf 8碼 public ...

字串編碼格式的轉換

主要知識點 檢查檔案或目錄是否存在1.file exists 2.is file 3.is dir 讀取檔案1.get contents將整個檔案讀取乙個引數 讀取內容少的時候建議使用 2.fopen開啟檔案或url 遮蔽錯誤資訊 3.fclose關閉開啟的檔案指標 4.fgets從檔案指標中讀取一...