面的兩段VB代碼分別針對UTF-8(UTF8ENCODEURI)和GB2312(GBKENCODEURI)進行了編碼的轉換。
PRIVATE SUB COMMAND1_CLICK()
DEBUG.PRINT (UTF8ENCODEURI("漢字"))
DEBUG.PRINT (GBKENCODEURI("漢字"))
END SUB
FUNCTION UTF8ENCODEURI(SZINPUT)
DIM WCH, UCH, SZRET
DIM X
DIM NASC, NASC2, NASC3
IF SZINPUT = "" THEN
UTF8ENCODEURI = SZINPUT
EXIT FUNCTION
END IF
FOR X = 1 TO LEN(SZINPUT)
WCH = MID(SZINPUT, X, 1)
NASC = ASCW(WCH)
IF NASC < 0 THEN NASC = NASC + 65536
IF (NASC AND &HFF80) = 0 THEN
SZRET = SZRET & WCH
ELSE
IF (NASC AND &HF000) = 0 THEN
UCH = "%" & HEX(((NASC 2 ^ 6)) OR &HC0) & HEX(NASC AND &H3F OR &H80)
SZRET = SZRET & UCH
ELSE
UCH = "%" & HEX((NASC 2 ^ 12) OR &HE0) & "%" & _
HEX((NASC 2 ^ 6) AND &H3F OR &H80) & "%" & _
HEX(NASC AND &H3F OR &H80)
SZRET = SZRET & UCH
END IF
END IF
NEXT
iconv() 函數
Definition and Usage
定義和用法
iconv()函數的作用是:轉換字串的編碼。
Description
string iconv ( string in_charset, string out_charset, string str )
Tips and Notes
注意點
注意:第二個參數,除了可以指定要轉化到的編碼以外,還可以增加兩個尾碼://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 會自動將不能直接轉化的字元變成一個或多個近似的字元,//IGNORE 會忽略掉不能轉化的字元,而預設效果是從第一個非法字元截斷。
mb_convert_encoding() 函數
Definition and Usage
定義和用法
mb_convert_encoding()函數的作用是:轉換字串的編碼。
Description
string mb_convert_encoding ( string str, string to-encoding [, mixed from-encoding])
注意:但是需要先enable mbstring 擴充庫。
兩者區別:mb_convert_encoding 中根據內容自動識別編碼;mb_convert_encoding功能強大,但是執行效率比iconv差太多;
總結:一般情況下用 iconv,只有當遇到無法確定原編碼是何種編碼時才用 mb_convert_encoding 函數.
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("你是我的好朋友", "gb312", "utf-8");
?>
信源:fleaphp.net
mb_convert_encoding 函數為php內部多位元組字串編碼轉換函式,可以在有需要的使用場合(如:解決在GB2312編碼環境下使用Ajax產生的中文字亂碼的問題)方便進行編碼轉換,以解決網頁亂碼的問題,使用非常方便,效率非常高,
幾乎支援所有編碼。PHP 4 >= 4.0.6、PHP 5 版本支援。
函數原型: 引用:
/**
* 多位元組字串編碼轉換函式
*
* @param string str 需要進行編碼轉換的字串
* @param string to_encoding 指定轉換為某種編碼,如:gb2312、gbk、utf-8等
* @param mixed from_encoding 混合指定原來字串的編碼,如:同時指定 JIS, eucjp-win, sjis-win 混合編碼
* @return string
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )使用舉例:
1、把 GBK 編碼字串轉換成 UTF-8 編碼字串 view plaincopy to clipboardprint?
<?php
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("你是我的好朋友", "UTF-8", "GBK");
?>
<?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("你是我的好朋友", "gb312", "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_http_output("HTML-ENTITIES");
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)擴充。
查看php的資訊頁面,如果出現如下畫面:
2008-10-16_111050.png (137.62 KB)
2008-10-16 12:01
則說明已啟用mbstring (multi-byte string)擴充支援。
如果沒有出現上面的畫面,則需要做如下設定,讓php支援該擴充。
1、windows 伺服器環境
編輯 php.ini 檔案,將; extension=php_mbstring.dll 前面的 ; 去掉,重啟網頁伺服器。
2、Linux伺服器環境
在編譯配置時加入 --enable-mbstring=cn 編譯參數,再進行PHP的編譯安裝。