爲了使JSON編碼解碼支持GB2312字符

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。

  1. <?php  
  2. /********Charset.php***********/  
  3. class Charset{  
  4.     //gb2312轉化爲utf-8  
  5.     function gb2utf8($chars){  
  6.         //使用iconv()函數,把gb2312字符,轉換爲utf-8的字符  
  7.         return iconv("gb2312","utf-8",$chars);  
  8.     }  
  9.     //gb2312轉化爲unicode  
  10.     function gb2unicode($chars){  
  11.         $string = "";  
  12.         preg_match_all("/[\x80-\xff]?./",$chars,$array);  
  13.         //遍歷字符串  
  14.         foreach($array[0] as $v){  
  15.             /** 
  16.              * 使用iconv()函數,把GB2312字符串轉化爲UTF-8編碼 
  17.              * 再使用utf8_unicode()函數,返回UTF-8字符的編碼值 
  18.              * 爲返回的編碼值,添加&#和;符號,形成unicode值 
  19.              * */  
  20.             $string .= "&#".$this->utf8_unicode(iconv("GB2312","UTF-8",$v)).";";  
  21.         }  
  22.         //返回最後的UNICODE值  
  23.         return $string;  
  24.     }  
  25.     //把單個utf-8字符轉換爲unicode數字值  
  26.     function utf8_unicode($c) {  
  27.     //根據字符的大小,返回字符串  
  28.     switch(strlen($c)) {  
  29.         case 1:  
  30.             return ord($c);  
  31.         case 2:  
  32.             $n = (ord($c[0]) & 0x3f) << 6;  
  33.             $n += ord($c[1]) & 0x3f;  
  34.             return $n;  
  35.         case 3:  
  36.             $n = (ord($c[0]) & 0x1f) << 12;  
  37.             $n += (ord($c[1]) & 0x3f) << 6;  
  38.             $n += ord($c[2]) & 0x3f;  
  39.             return $n;  
  40.         case 4:  
  41.             $n = (ord($c[0]) & 0x0f) << 18;  
  42.             $n += (ord($c[1]) & 0x3f) << 12;  
  43.             $n += (ord($c[2]) & 0x3f) << 6;  
  44.             $n += ord($c[3]) & 0x3f;  
  45.             return $n;  
  46.         }  
  47.     }  
  48.     //utf-8轉爲了gb2312編碼  
  49.     function utf82gb($chars){  
  50.         //使用iconv()函數,把utf-8編碼,轉化爲gb2312編碼  
  51.         return iconv("utf-8","gb2312",$chars);  
  52.     }  
  53.     //utf-8編碼,轉化爲unicode編碼  
  54.     function utf82unicode($chars){  
  55.         //使用utf82gb()函數,返回字符的GB值  
  56.         $utf8 = $this->utf82gb($chars);  
  57.         //再使用gb2unicode()函數,返回字符的unicode值  
  58.         return $this->gb2unicode($utf8);  
  59.     }  
  60.     //unicode編碼轉化爲utf-8編碼  
  61.     function unicode2utf8($chars){  
  62.         $string = "";  
  63.         //把unicode編碼的字符串進行分割  
  64.         $chars = explode(";",$chars);  
  65.         //遍歷分割後的字符串  
  66.         foreach($chars as $char){  
  67.             //取得unicode編碼中的數字值  
  68.             $unicode = substr($char,2);  
  69.             //使用unicode_utf8()函數,返回這個值對應的utf-8字符  
  70.             $string .= $this->unicode_utf8($unicode);  
  71.         }  
  72.         //返回最後的utf-8字符串  
  73.         return $string;  
  74.     }  
  75.     //unicode轉爲了gb2312編碼  
  76.     function unicode2gb($chars){  
  77.         //使用unicode2utf8函數,返回與utf-8對應的字符  
  78.         $string = $this->unicode2utf8($chars);  
  79.         //再使用utf82gb()函數,返加GB2312編碼的字符串  
  80.         return $this->utf82gb($string);  
  81.     }  
  82.     //把單個unicode數字值,轉換爲utf-8字符  
  83.     function unicode_utf8($c){  
  84.         $str="";  
  85.         //根據unicode數字值,計算並返回字符  
  86.         if($c < 0x80){  
  87.             $str.=$c;  
  88.         }elseif($c < 0x800){  
  89.             $str.=chr(0xC0 | $c>>6);  
  90.             $str.=chr(0x80 | $c & 0x3F);  
  91.         }elseif($c < 0x10000){  
  92.             $str.=chr(0xE0 | $c>>12);  
  93.             $str.=chr(0x80 | $c>>6 & 0x3F);  
  94.             $str.=chr(0x80 | $c & 0x3F);  
  95.         }elseif($c < 0x200000){  
  96.             $str.=chr(0xF0 | $c>>18);  
  97.             $str.=chr(0x80 | $c>>12 & 0x3F);  
  98.             $str.=chr(0x80 | $c>>6 & 0x3F);  
  99.             $str.=chr(0x80 | $c & 0x3F);  
  100.         }  
  101.         return $str;  
  102.     }  
  103. }  
  104. ?>  

例子:

  1. <!---------------------------------------文件名: 6_3.php-------------------------------->  
  2. <?php  
  3. //爲了使用中JSON編碼解碼支持GB2312字符  
  4. //可以包含charset字符編碼轉換類,來實現字符之間的轉換  
  5. include_once("Charset.php");  
  6. //包含JSON編碼解碼類  
  7. include_once("json.php");  
  8. //初始化字符編碼解碼類  
  9. $charset = new Charset();  
  10. //初始化JSON編碼解碼類  
  11. $json = new JSON();  
  12. //定義需要編碼的數組  
  13. $users = array(  
  14.     array("username"=>$charset->gb2unicode("中文"),"password"=>"1","style"=>"css1"),  
  15.     array("username"=>"jake","password"=>"2","style"=>"css2")  
  16. );  
  17. //使用JSON類中的encode()函數進行編碼  
  18. $json_data = $json->encode($users);  
  19. echo $json_data;  
  20. ?>
發佈了12 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章