为了使JSON编码解码支持GB2312字符

版权声明:本文为博主原创文章,未经博主允许不得转载。

[php]  view plain copy
  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. ?>  

例子:

[php]  view plain copy
  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. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值