将 &#36152&#26131 转换成gb2312中文编码

实现代码如下:
$str = "&#36152&#26131&#32463&#27982";

echo recode($str);

function recode($str)
{
$str 
= preg_replace("|&#([0-9]{1,5})|""\".u2utf82gb(\\1).\"", $str);
$str 
= "\$str=\"$str\";";

eval($str);
return $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);
}

posted @ 2005-09-08 09:11  小强(占卜师)  阅读(542)  评论(0)    收藏  举报