php字串編碼轉換

2021-06-19 17:16:42 字數 1215 閱讀 5948

使用舉例:

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

<?php 

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

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

?>

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

// 注意將此檔案存檔成 

utf-8編碼格式檔案再測試 

<?php 

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

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

?>

// 注意將此檔案存檔成 

utf-8編碼格式檔案再測試

<?php

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

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

?>

3、對整個頁面進行轉換

該方法適用所有編碼環境。這樣把前128個字元以外(顯示字元)的字符集都用 

ncr(numeric characterreference,如「漢字」將轉換成「漢字」這種形式)來表示,這樣的編碼在任意編碼環境下頁面都能正常顯示。

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

view plaincopy to clipboardprint?

mb_internal_encoding(」gb2312″);  //這裡的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 to clipboardprint?header content type text html charset utf 8 echo mb convert encoding 你是我的好朋友 utf 8 gbk 2 把...

PHP字串檢測編碼格式並轉換

檢測編碼格式並轉換需要兩個函式來實現 1.mb detect encoding 檢測字元的編碼 說明string mb detect encoding string str mixed encoding list mb detect order bool strict false 檢測字串 str ...

java 字串編碼轉換

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