各類平臺和軟件打開顯示的編碼問題,須要使用不一樣的編碼,根據咱們不一樣的需求。php
php 字符編碼轉換類,支持ANSI、Unicode、Unicode big endian、UTF-八、UTF-8+Bom 互相轉換。ide
四種常見文本文件編碼方式ui
ANSI編碼:this
無文件頭(文件編碼開頭標誌性字節)編碼
ANSI編碼字母數字佔一個字節,漢字佔兩個字節spa
回車換行符,單字節, 十六進制表示爲0d 0acode
UNICODE編碼:blog
文件頭,十六進制表示爲FF FEip
每個字符都用兩個字節編碼utf-8
回車換行符, 雙字節,十六進制表示爲 000d 000a
Unicode big endian編碼:
文件頭十六進制表示爲FE FF
後面編碼是把字符的高位放在前面,低位放在後面,正好和Unicode編碼顛倒
回車換行符,雙字節,十六進制表示爲0d00 0a00
UTF-8 編碼:
文件頭,十六進制表示爲EF BB BF
UTF-8是Unicode的一種變長字符編碼,數字、字母、回車、換行都用一個字節表示,漢字佔3個字節
回車換行符,單字節,十六進制表示爲0d 0a
轉換原理:先把字符編碼轉爲UTF-8,而後再從UTF-8轉換爲對應的字符編碼。
CharsetConv.class.php:
1 <?php 2 /**字符編碼轉換類, ANSI、Unicode、Unicode big endian、UTF-八、UTF-8+Bom互相轉換 3 *Date: 2015-01-28 4 *Author: fdipzone 5 *Ver: 1.0 6 * 7 *Func: 8 *public convert 轉換 9 *private convToUtf8 把編碼轉爲UTF-8編碼 10 *private convFromUtf8 把UTF-8編碼轉換爲輸出編碼 11 */ 12 13 class CharsetConv{ // class start 14 15 private $_in_charset = null; // 源編碼 16 private $_out_charset = null; // 輸出編碼 17 private $_allow_charset = array('utf-8', 'utf-8bom', 'ansi', 'unicode', 'unicodebe'); 18 19 /**初始化 20 * @param String $in_charset 源編碼 21 * @param String $out_charset 輸出編碼 22 */ 23 public function __construct($in_charset, $out_charset){ 24 25 $in_charset = strtolower($in_charset); 26 $out_charset = strtolower($out_charset); 27 28 // 檢查源編碼 29 if(in_array($in_charset, $this->_allow_charset)){ 30 $this->_in_charset = $in_charset; 31 } 32 33 // 檢查輸出編碼 34 if(in_array($out_charset, $this->_allow_charset)){ 35 $this->_out_charset = $out_charset; 36 } 37 38 } 39 40 /**轉換 41 * @param String $str 要轉換的字符串 42 * @return String 轉換後的字符串 43 */ 44 public function convert($str){ 45 46 $str = $this->convToUtf8($str); // 先轉爲utf8 47 $str = $this->convFromUtf8($str); // 從utf8轉爲對應的編碼 48 49 return $str; 50 } 51 52 /**把編碼轉爲UTF-8編碼 53 * @param String $str 54 * @return String 55 */ 56 private function convToUtf8($str){ 57 58 if($this->_in_charset=='utf-8'){ // 編碼已是utf-8,不用轉 59 return $str; 60 } 61 62 switch($this->_in_charset){ 63 case 'utf-8bom': 64 $str = substr($str, 3); 65 break; 66 67 case 'ansi': 68 $str = iconv('GBK', 'UTF-8//IGNORE', $str); 69 break; 70 71 case 'unicode': 72 $str = iconv('UTF-16le', 'UTF-8//IGNORE', substr($str, 2)); 73 break; 74 75 case 'unicodebe': 76 $str = iconv('UTF-16be', 'UTF-8//IGNORE', substr($str, 2)); 77 break; 78 79 default: 80 break; 81 } 82 83 return $str; 84 85 } 86 87 /**把UTF-8編碼轉換爲輸出編碼 88 * @param String $str 89 * @return String 90 */ 91 private function convFromUtf8($str){ 92 93 if($this->_out_charset=='utf-8'){ // 輸出編碼已是utf-8,不用轉 94 return $str; 95 } 96 97 switch($this->_out_charset){ 98 case 'utf-8bom': 99 $str = "\xef\xbb\xbf".$str; 100 break; 101 102 case 'ansi': 103 $str = iconv('UTF-8', 'GBK//IGNORE', $str); 104 break; 105 106 case 'unicode': 107 $str = "\xff\xfe".iconv('UTF-8', 'UTF-16le//IGNORE', $str); 108 break; 109 110 case 'unicodebe': 111 $str = "\xfe\xff".iconv('UTF-8', 'UTF-16be//IGNORE', $str); 112 break; 113 114 default: 115 break; 116 } 117 118 return $str; 119 120 } 121 122 } // class end 123 124 ?>
demo:unicode big endian 轉爲 utf-8+bom:
1 <?php 2 require "CharsetConv.class.php"; 3 4 $str = file_get_contents('source/unicodebe.txt'); 5 6 $obj = new CharsetConv('unicodebe', 'utf-8bom'); 7 $response = $obj->convert($str); 8 9 file_put_contents('response/utf-8bom.txt', $response, true); 10 ?>