免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2222 | 回复: 2
打印 上一主题 下一主题

帮我看看utf8转unicode函数对不对????????????? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-08 11:00 |只看该作者 |倒序浏览
function utf2unicode($string)
{
   $tempstr = $string;
   #$tempstr= trim($tempstr);
   $len = strlen($tempstr);
   $outstr="";
   for ($i=0;$i<$len;$i++)
   {

       if (ord(substr($tempstr,$i,1)) < 128 )
       {
           $a = chr(ord(substr($tempstr,$i,1)));
           $outstr=$outstr . $a;
       }
       elseif (ord(substr($tempstr,$i,1)) >;= 192 and ord(substr($tempstr,$i,1)) < 224)
       {
          $er = $i + 1;
          $outsub = bindec(str_pad (decbin(ord(substr($tempstr,$i,1))-192), 11, decbin(ord(substr($tempstr,$er,1))-12));
          $outsub = dechex($outsub);
          $outstr =$outstr."&#x".$outsub.";";
          $i++;
       }
       elseif (ord(substr($tempstr,$i,1)) >;= 224 and ord(substr($tempstr,$i,1)) < 239)
       {
          $er = $i + 1;
          $san = $i + 2;
          #$sbs = str_pad (decbin(ord(substr($tempstr,$i,1)) - 224), 10, decbin(ord(substr($tempstr,$er,1)) - 12);
          $outsub = bindec(str_pad (str_pad (decbin(ord(substr($tempstr,$i,1))-224), 10, decbin(ord(substr($tempstr,$er,1))-12),16,decbin(ord(substr($tempstr,$san,1))-12));
          $outsub = dechex($outsub);
          $outstr = $outstr."&#x".$outsub.";";
          $i= $i + 2;
       }
       else
       {
          $er = $i + 1;
          $san = $i + 2;
          $si = $i + 3;

          $diyibu=str_pad (decbin(ord(substr($tempstr,$i,1))-240), 9, decbin(ord(substr($tempstr,$er,1))-12);
          $dierbu = str_pad($diyibu,15,decbin(ord(substr($tempstr,$san,1))-12);
          $disanbu = str_pad ($dierbu,21,decbin(ord(substr($tempstr,$si,1))-12);
          $outstr = bindec($disanbu);
          $outsub = dechex($outsub);
          $outstr = $outstr."&#x".$outsub.";";
          $i= $i + 3;
       }


   }
   return $outstr;
}

论坛徽章:
0
2 [报告]
发表于 2003-12-09 18:07 |只看该作者

帮我看看utf8转unicode函数对不对?????????????

我已经搞定啦
  1. <?
  2. #################################################################
  3. function gb2unicode($gb)
  4. {
  5.    if(!trim($gb))
  6.    return $gb;
  7.    $filename="gb2312.txt";
  8.    $tmp=file($filename);
  9.    $codetable=array();
  10.    while(list($key,$value)=each($tmp))
  11.    $codetable[hexdec(substr($value,0,6))]=substr($value,9,4);
  12.    $utf="";
  13.    while($gb)
  14.    {
  15.      if (ord(substr($gb,0,1))>;127)
  16.      {
  17.        $this=substr($gb,0,2);
  18.        $gb=substr($gb,2,strlen($gb));
  19.        $utf.="&#x".$codetable[hexdec(bin2hex($this))-0x8080].";";
  20.      }
  21.      else
  22.      {
  23.      $gb=substr($gb,1,strlen($gb));
  24.      $utf.=substr($gb,0,1);
  25.      }
  26.      }
  27.   return $utf;
  28. }
  29. ############################################################################
  30. function utf2unicode($string)
  31. {
  32.     $string=urlencode($string);
  33.     $outstring="";
  34.     $strarray = split("%",$string);
  35.     for ($i=0;$i<count($strarray);$i++)
  36.     {
  37.         if (hexdec($strarray[$i]) < 128)
  38.         {
  39.             if (hexdec($strarray[$i]))
  40.             $outstring =$outstring . chr(hexdec($strarray[$i]));
  41.         }
  42.         if (hexdec($strarray[$i]) >;= 224 and hexdec($strarray[$i]) < 239)
  43.         {
  44.             $charb1 = strval(decbin(hexdec($strarray[$i])) - 11100000);
  45.             $charb1 = str_pad($charb1,4,"0",STR_PAD_LEFT);
  46.             $i++;
  47.             $charb2 = strval(decbin(hexdec($strarray[$i])) - 10000000);
  48.             $charb2 = str_pad($charb2,6,"0",STR_PAD_LEFT);
  49.             $i++;
  50.             $charb3 = strval(decbin(hexdec($strarray[$i])) - 10000000);
  51.             $charb3 = str_pad($charb3,6,"0",STR_PAD_LEFT);
  52.             $pad1 = str_pad($charb1,10,$charb2);
  53.             $outbin = str_pad($pad1,16,$charb3);
  54.             $outsub = "&#".bindec($outbin).";";
  55.             $outstring = $outstring . $outsub;
  56.         }
  57.     }
  58. return $outstring;
  59. }
  60. ?>;
复制代码

论坛徽章:
0
3 [报告]
发表于 2003-12-09 22:27 |只看该作者

帮我看看utf8转unicode函数对不对?????????????

iconv()
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP