版權聲明:本文爲博主原創文章,未經博主允許不得轉載。
- <?php
- /********Charset.php***********/
- class Charset{
- //gb2312轉化爲utf-8
- function gb2utf8($chars){
- //使用iconv()函數,把gb2312字符,轉換爲utf-8的字符
- return iconv("gb2312","utf-8",$chars);
- }
- //gb2312轉化爲unicode
- function gb2unicode($chars){
- $string = "";
- preg_match_all("/[\x80-\xff]?./",$chars,$array);
- //遍歷字符串
- foreach($array[0] as $v){
- /**
- * 使用iconv()函數,把GB2312字符串轉化爲UTF-8編碼
- * 再使用utf8_unicode()函數,返回UTF-8字符的編碼值
- * 爲返回的編碼值,添加&#和;符號,形成unicode值
- * */
- $string .= "&#".$this->utf8_unicode(iconv("GB2312","UTF-8",$v)).";";
- }
- //返回最後的UNICODE值
- return $string;
- }
- //把單個utf-8字符轉換爲unicode數字值
- function utf8_unicode($c) {
- //根據字符的大小,返回字符串
- switch(strlen($c)) {
- case 1:
- return ord($c);
- case 2:
- $n = (ord($c[0]) & 0x3f) << 6;
- $n += ord($c[1]) & 0x3f;
- return $n;
- case 3:
- $n = (ord($c[0]) & 0x1f) << 12;
- $n += (ord($c[1]) & 0x3f) << 6;
- $n += ord($c[2]) & 0x3f;
- return $n;
- case 4:
- $n = (ord($c[0]) & 0x0f) << 18;
- $n += (ord($c[1]) & 0x3f) << 12;
- $n += (ord($c[2]) & 0x3f) << 6;
- $n += ord($c[3]) & 0x3f;
- return $n;
- }
- }
- //utf-8轉爲了gb2312編碼
- function utf82gb($chars){
- //使用iconv()函數,把utf-8編碼,轉化爲gb2312編碼
- return iconv("utf-8","gb2312",$chars);
- }
- //utf-8編碼,轉化爲unicode編碼
- function utf82unicode($chars){
- //使用utf82gb()函數,返回字符的GB值
- $utf8 = $this->utf82gb($chars);
- //再使用gb2unicode()函數,返回字符的unicode值
- return $this->gb2unicode($utf8);
- }
- //unicode編碼轉化爲utf-8編碼
- function unicode2utf8($chars){
- $string = "";
- //把unicode編碼的字符串進行分割
- $chars = explode(";",$chars);
- //遍歷分割後的字符串
- foreach($chars as $char){
- //取得unicode編碼中的數字值
- $unicode = substr($char,2);
- //使用unicode_utf8()函數,返回這個值對應的utf-8字符
- $string .= $this->unicode_utf8($unicode);
- }
- //返回最後的utf-8字符串
- return $string;
- }
- //unicode轉爲了gb2312編碼
- function unicode2gb($chars){
- //使用unicode2utf8函數,返回與utf-8對應的字符
- $string = $this->unicode2utf8($chars);
- //再使用utf82gb()函數,返加GB2312編碼的字符串
- return $this->utf82gb($string);
- }
- //把單個unicode數字值,轉換爲utf-8字符
- function unicode_utf8($c){
- $str="";
- //根據unicode數字值,計算並返回字符
- if($c < 0x80){
- $str.=$c;
- }elseif($c < 0x800){
- $str.=chr(0xC0 | $c>>6);
- $str.=chr(0x80 | $c & 0x3F);
- }elseif($c < 0x10000){
- $str.=chr(0xE0 | $c>>12);
- $str.=chr(0x80 | $c>>6 & 0x3F);
- $str.=chr(0x80 | $c & 0x3F);
- }elseif($c < 0x200000){
- $str.=chr(0xF0 | $c>>18);
- $str.=chr(0x80 | $c>>12 & 0x3F);
- $str.=chr(0x80 | $c>>6 & 0x3F);
- $str.=chr(0x80 | $c & 0x3F);
- }
- return $str;
- }
- }
- ?>
例子:
- <!---------------------------------------文件名: 6_3.php-------------------------------->
- <?php
- //爲了使用中JSON編碼解碼支持GB2312字符
- //可以包含charset字符編碼轉換類,來實現字符之間的轉換
- include_once("Charset.php");
- //包含JSON編碼解碼類
- include_once("json.php");
- //初始化字符編碼解碼類
- $charset = new Charset();
- //初始化JSON編碼解碼類
- $json = new JSON();
- //定義需要編碼的數組
- $users = array(
- array("username"=>$charset->gb2unicode("中文"),"password"=>"1","style"=>"css1"),
- array("username"=>"jake","password"=>"2","style"=>"css2")
- );
- //使用JSON類中的encode()函數進行編碼
- $json_data = $json->encode($users);
- echo $json_data;
- ?>