asp(vbs),php,解码&#;utf8编码

 str0= "这是一个utf-8字符串! "
function   reg_arr(reg_str,str,ml)
Set   re   =   New   RegExp
re.Pattern   =   reg_str
re.Global   =   True
re.IgnoreCase   =   True
re.MultiLine   =   True
set   m=re.execute(str)
dim   rtn
dim   tmp
redim   rtn(m.count-1)
i=0
for   each   mt   in   m
mv=mt.value
redim   tmp(ml)
for   k=1   to   ml
par= "$ "&k
tmp(k-1)=re.replace(mv,par)
next
rtn(i)=tmp
i=i+1
next
reg_arr=rtn
end   function
function   utf8code2gb(str)
strs=reg_arr( "&#(/d+); ",str,1)
for   i=0   to   ubound(strs)
str=replace(str, "&# "&strs(i)(0)& "; ",chrw(strs(i)(0)))
next
utf8code2gb=str
end   function
msgbox(utf8code2gb(str0))

%E6%8C%A8%E6%89%93用的是ascii编码的16进制
挨打是unicode编码

 

<?php
$str = "&#32769;&#22823;&#21733;";
$str = preg_replace("|&#([0-9]{1,5});|", "/".u2utf82gb(//1)./"", $str);
$str = "/$str=/"$str/";";

eval($str);
echo $str;

function u2utf82gb($c){
    $str="";
    if ($c < 0x80) {
         $str.=$c;
    } else if ($c < 0x800) {
         $str.=chr(0xC0 | $c>>6);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x10000) {
         $str.=chr(0xE0 | $c>>12);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($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 iconv('UTF-8', 'GB2312', $str);
}
?>

 

再加一个手册上的例子,解决:%u697C%u4E3B%u8111%u58F3%u53D1%u660F%u7528%u9519%u8BCD%u4E86%u5427%3F%20%0D%0A%u4F60%u53EF%u4EE5%u770B%u4E0D%u8D77%u9C81%u8FC5%2C%u4F46%u4F60%u4E0D%u53EF%u4EE5%u7528%u4ED6%u6765%u5F62%u5BB9%u8DD1

function unicode_urldecode($url)
{
   preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);
  
   foreach ($a[1] as $uniord)
   {
       $dec = hexdec($uniord);
       $utf = '';
      
       if ($dec < 128)
       {
           $utf = chr($dec);
       }
       else if ($dec < 2048)
       {
           $utf = chr(192 + (($dec - ($dec % 64)) / 64));
           $utf .= chr(128 + ($dec % 64));
       }
       else
       {
           $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
           $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
           $utf .= chr(128 + ($dec % 64));
       }
      
       $url = str_replace('%u'.$uniord, $utf, $url);
   }
  
   return urldecode($url);
}

posted @ 2008-02-27 15:49  MultiThread-PHP  阅读(575)  评论(0编辑  收藏  举报