函数转变编码就相比较比较轻巧了
分类:热门资源

假如使用 iconv() 函数转换编码就相比比较简单了,不过很多虚拟主机里并不支持这个组件,我在网上找半天,才找到一个gb2312转utf-8的方法,但不能逆向转换。这个函数如下:/*******************************//GB转UTF-8编码*******************************/function gb2utf8($gbstr) {global $CODETABLE;if(trim($gbstr)==) return $gbstr;if(empty($CODETABLE)){$filename = dirname(__FILE__)./gb2312-utf8.table;$fp = fopen($filename,r);while ($l = fgets($fp,15)){ $CODETABLE[hexdec(substr($l, 0, 6))] = substr($l, 7, 6); }fclose($fp);}$ret = ;$utf8 = ;while ($gbstr) {if (ord(substr($gbstr, 0, 1)) 127) {$thisW = substr($gbstr, 0, 2);$gbstr = substr($gbstr, 2, strlen($gbstr));$utf8 = ;@$utf8 = u2utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW)) - 0x8080]));if($utf8!=){for ($i = 0;$i strlen($utf8);$i = 3)$ret .= chr(substr($utf8, $i, 3));}}else{$ret .= substr($gbstr, 0, 1);$gbstr = substr($gbstr, 1, strlen($gbstr));}}return $ret;}//Unicode转utf8function u2utf8($c) {for ($i = 0;$i count($c);$i )$str = ;if ($c 0x80) {$str .= $c;} else if ($c 0x800) {$str .= (0xC0 | $c 6);$str .= (0x80 | $c 0x3F);} else if ($c 0x10000) {$str .= (0xE0 | $c 12);$str .= (0x80 | $c 6 0x3F);$str .= (0x80 | $c 0x3F);} else if ($c 0x200000) {$str .= (0xF0 | $c 18);$str .= (0x80 | $c 12 0x3F);$str .= (0x80 | $c 6 0x3F);$str .= (0x80 | $c 0x3F);}

$uc2gbtable = $codetable = $big5_data = $gb_data = '';
$gbkunidic = null;

//utf-8 转gb编码
function utf82gb($utfstr)
{
 if(function_exists('iconv'))
 {
  return iconv('utf-8','gbk//ignore',$utfstr);
 }
 global $uc2gbtable;
 $okstr = "";
 if(trim($utfstr)=="")
 {
  return $utfstr;
 }
 if(empty($uc2gbtable))
 {
  $filename = dedeinc."/data/gb2312-utf8.dat";
  $fp = fopen($filename,"r");
  while($l = fgets($fp,15))
  {
   $uc2gbtable[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
  }
  fclose($fp);
 }
 $okstr = "";
 $ulen = strlen($utfstr);
 for($i=0;$i<$ulen;$i++)
 {
  $c = $utfstr[$i];
  $cb = decbin(ord($utfstr[$i]));
  if(strlen($cb)==8)
  {
   $csize = strpos(decbin(ord($cb)),"0");
   for($j=0;$j < $csize;$j++)
   {
    $i++; $c .= $utfstr[$i];
   }
   $c = utf82u($c);
   if(isset($uc2gbtable[$c]))
   {
    $c = dechex($uc2gbtable[$c]+0x8080);
    $okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
   }
   else
   {
    $okstr .= "&#".$c.";";
   }
  }
  else
  {
   $okstr .= $c;
  }
 }
 $okstr = trim($okstr);
 return $okstr;
}

 

 

 

//gb转utf-8编码
function gb2utf8($gbstr)
{
 if(function_exists('iconv'))
 {
  return iconv('gbk','utf-8//ignore',$gbstr);
 }
 global $codetable;
 if(trim($gbstr)=="")
 {
  return $gbstr;
 }
 if(empty($codetable))
 {
  $filename = dedeinc."/data/gb2312-utf8.dat";
  $fp = fopen($filename,"r");
  while ($l = fgets($fp,15))
  {
   $codetable[hexdec(substr($l, 0, 6))] = substr($l, 7, 6);
  }
  fclose($fp);
 }
 $ret = "";
 $utf8 = "";
 while ($gbstr != '')
 {
  if (ord(substr($gbstr, 0, 1)) > 0x80)
  {
   $thisw = substr($gbstr, 0, 2);
   $gbstr = substr($gbstr, 2, strlen($gbstr));
   $utf8 = "";
   @$utf8 = u2utf8(hexdec($codetable[hexdec(bin2hex($thisw)) - 0x8080]));
   if($utf8!="")
   {
    for ($i = 0;$i < strlen($utf8);$i += 3)
    $ret .= chr(substr($utf8, $i, 3));
   }
  }
  else
  {
   $ret .= substr($gbstr, 0, 1);
   $gbstr = substr($gbstr, 1, strlen($gbstr));
  }
 }
 return $ret;
}

 

 

 

//unicode转utf8
function u2utf8($c)
{
 for ($i = 0;$i < count($c);$i++)
 {
  $str = "";
 }
 if ($c < 0x80)
 {
  $str .= $c;
 }
 else if ($c < 0x800)
 {
  $str .= (0xc0 | $c >> 6);
  $str .= (0x80 | $c & 0x3f);
 }
 else if ($c < 0x10000)
 {
  $str .= (0xe0 | $c >> 12);
  $str .= (0x80 | $c >> 6 & 0x3f);
  $str .= (0x80 | $c & 0x3f);
 }
 else if ($c < 0x200000)
 {
  $str .= (0xf0 | $c >> 18);
  $str .= (0x80 | $c >> 12 & 0x3f);
  $str .= (0x80 | $c >> 6 & 0x3f);
  $str .= (0x80 | $c & 0x3f);
 }
 return $str;
}

//utf8转unicode
function utf82u($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;
 }
}

 

 

 

//big5码转换成gb码
function big52gb($text)
{
 if(function_exists('iconv'))
 {
  return iconv('big5','gbk//ignore',$text);
 }
 global $big5_data;
 if(empty($big5_data))
 {
  $filename = dedeinc."/data/big5-gb.dat";
  $fp = fopen($filename, "rb");
  $big5_data = fread($fp,filesize($filename));
  fclose($fp);
 }
 $max = strlen($text)-1;
 for($i=0;$i<$max;$i++)
 {
  $h = ord($text[$i]);
  if($h>=0x80)
  {
   $l = ord($text[$i+1]);
   if($h==161 && $l==64)
   {
    $gbstr = " ";
   }
   else
   {
    $p = ($h-160)*510+($l-1)*2;
    $gbstr = $big5_data[$p].$big5_data[$p+1];
   }
   $text[$i] = $gbstr[0];
   $text[$i+1] = $gbstr[1];
   $i++;
  }
 }
 return $text;
}

 

 

 

//gb码转换成big5码
function gb2big5($text)
{
 if(function_exists('iconv'))
 {
  return iconv('gbk','big5//ignore',$text);
 }
 global $gb_data;
 if(empty($gb_data))
 {
  $filename = dedeinc."/data/gb-big5.dat";
  $fp = fopen($filename, "rb");
  $gb = fread($fp,filesize($filename));
  fclose($fp);
 }
 $max = strlen($text)-1;
 for($i=0;$i<$max;$i++)
 {
  $h = ord($text[$i]);
  if($h>=0x80)
  {
   $l = ord($text[$i+1]);
   if($h==161 && $l==64)
   {
    $big = " ";
   }
   else
   {
    $p = ($h-160)*510+($l-1)*2;
    $big = $gb_data[$p].$gb_data[$p+1];
   }
   $text[$i] = $big[0];
   $text[$i+1] = $big[1];
   $i++;
  }
 }
 return $text;
}

 

 

 

//unicode url编码转gbk编码函数
function unicodeurl2gbk($str)
{
 //载入对照词典
 if(!isset($globals['gbkunidic']))
 {
  $fp = fopen(dedeinc.'/data/gbk-unicode.dat','rb');
  while(!feof($fp))
  {
   $globals['gbkunidic'][bin2hex(fread($fp,2))] = fread($fp,2);
  }
  fclose($fp);
 }

 //处理字符串
 $str = str_replace('$#$','+',$str);
 $glen = strlen($str);
 $okstr = "";
 for($i=0; $i < $glen; $i++)
 {
  if($glen-$i > 4)
  {
   if($str[$i]=='%' && $str[$i+1]=='u')
   {
    $uni = strtolower(substr($str,$i+2,4));
    $i = $i+5;
    if(isset($globals['gbkunidic'][$uni]))
    {
     $okstr .= $globals['gbkunidic'][$uni];
    }
    else
    {
     $okstr .= "&#".hexdec('0x'.$uni).";";
    }
   }
   else
   {
    $okstr .= $str[$i];
   }
  }
  else
  {
   $okstr .= $str[$i];
  }
 }
 return $okstr;
}

本文由威尼斯在线注册平台发布于热门资源,转载请注明出处:函数转变编码就相比较比较轻巧了

上一篇:威尼斯正规官网php删除目录和文件 下一篇:没有了
猜你喜欢
热门排行
精彩图文