目前共有5篇帖子。 内容转换:不转换▼
 
点击 回复
202 4
PHP实现gb2312、UTF-8等字符和unicode间的编码转换及PHP版unescape
一派护法 十九级
1楼 发表于:2010-10-17 22:13

<?
/**
 * 将字符串转换成unicode编码
 *
 * @param string $input
 * @param string $input_charset
 * @return string
 */
function str_to_unicode($input, $input_charset = 'gbk'){
 $input = iconv($input_charset, "gbk", $input);
 preg_match_all("/[\x80-\xff]?./", $input, $ar);
 $b = array_map('utf8_unicode_', $ar[0]);
 $outstr = join("", $b);
 return $outstr;
}[水平网:http://goalercn.com]

function utf8_unicode_($c, $input_charset = 'gbk'){
 $c = iconv($input_charset, 'utf-8', $c);
 return utf8_unicode($c);
}
// utf8 -> unicode
function utf8_unicode($c) {
 switch(strlen($c)) {
 case 1:
 return $c;
 case 2:
 $n = (ord($c[0]) & 0x3f) << 6;
 $n += ord($c[1]) & 0x3f;
 break;
 case 3:
 $n = (ord($c[0]) & 0x1f) << 12;
 $n += (ord($c[1]) & 0x3f) << 6;
 $n += ord($c[2]) & 0x3f;
 break;
 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;
 break;
 }
 return "&#$n;";
}[水平网:http://goalercn.com]

/**
 * 将unicode字符转换成普通编码字符
 *
 * @param string $str
 * @param string $out_charset
 * @return string
 */
function str_from_unicode($str, $out_charset = 'gbk'){
 $str = preg_replace_callback("|&#([0-9]{1,5});|", 'unicode2utf8_', $str);
 $str = iconv("UTF-8", $out_charset, $str);
 return $str;
}[水平网:http://goalercn.com]

function unicode2utf8_($c){
 return unicode2utf8($c[1]);
}
function unicode2utf8($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 $str;
}[水平网:http://goalercn.com]

/**
 * 模拟JS里的unescape
 *
 * @param unknown_type $str
 * @return unknown
 */
function unescape($str) {
 $str = rawurldecode($str);
 preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);
 $ar = $r[0];
 #print_r($ar);
 foreach($ar as $k=>$v) {
 if(substr($v,0,2) == "%u")
 $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
 elseif(substr($v,0,3) == "&#x")
 $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
 elseif(substr($v,0,2) == "&#") {
 echo substr($v,2,-1)."
";
 $ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
 }
 }
 return join("",$ar);
}
?>[水平网:http://goalercn

本文转自[水平网]:http://www.goalercn.com/article/article.php?id=2620

一派护法 十九级
2楼 发表于:2010-10-17 22:13
<?    
function phpescape($str)
{
         $sublen=strlen($str);
         $retrunString="";
         for ($i=0;$i<$sublen;$i++)
         {
                  if(ord($str[$i])>=127)
                  {
                           $tmpString=bin2hex(iconv("gb2312","ucs-2",substr($str,$i,2)));
                           //$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);window下可能要打开此项
                           $retrunString.="%u".$tmpString;
                           $i++;
                  } else {
                           $retrunString.="%".dechex(ord($str[$i]));
                  }
         }
         return $retrunString;

function unescape($str) {
         $str = rawurldecode($str);
         preg_match_all("/%u.{4}|&#x.{4};|&#\d+;|.+/U",$str,$r);
         $ar = $r[0];
         foreach($ar as $k=>$v) {
                  if(substr($v,0,2) == "%u")
                           $ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,-4)));
                  elseif(substr($v,0,3) == "&#x")
                           $ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,3,-1)));
                  elseif(substr($v,0,2) == "&#") {
                           $ar[$k] = iconv("UCS-2","GBK",pack("n",substr($v,2,-1)));
                  }
         }
         return join("",$ar);
}    
?>
一派护法 十九级
3楼 发表于:2010-10-17 22:14
直接调用吧!

在javascript里面使用escape对地址进行加密,通过php的自定义函数进行解密;
或者在php中通过自定义escape函数加密,在javascript里面进行解密;
完成服务器与客服端之间的通信!
view plaincopy to clipboardprint? function escape($str)    {        preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);        $ar = $r[0];        foreach($ar as $k=>$v)         {            if(ord($v[0]) < 128)                $ar[$k] = rawurlencode($v);            else                $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));        }        return join("",$ar);    }   function escape($str) { preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if(ord($v[0]) < 128) $ar[$k] = rawurlencode($v); else $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v)); } return join("",$ar); }
view plaincopy to clipboardprint? function unescape($str)     {        $str = rawurldecode($str);        preg_match_all("/(?:%u.{4})|.+/",$str,$r);        $ar = $r[0];        foreach($ar as $k=>$v)         {            if(substr($v,0,2) == "%u" && strlen($v) == 6)                $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));        }        return join("",$ar);    }  
一派护法 十九级
4楼 发表于:2010-10-17 22:14
一派护法 十九级
5楼 发表于:2010-10-17 22:15

http://zhidao.baidu.com/question/166890167.html?si=2

a.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<script language="JavaScript">

function GetResult(str) {

var oBao = new ActiveXObject("Microsoft.XMLHTTP");

oBao.open("POST","check.php?str="+str,false);

oBao.send();

//服务器端处理返回的是经过escape编码的字符串.

document.getElementById("re").innerHTML=unescape(oBao.responseText);
//alert(unescape(oBao.responseText));
//alert(str);
}
</script>

<table><tr><td>用户名:</td><td><input type="text" name="text1" value="" onblur="GetResult(this.value)"></td><td><div id="re"></div></td></tr></table>


check.php

<?php
function js_unescape($str)
{
        $ret = '';
        $len = strlen($str);

        for ($i = 0; $i < $len; $i++)
        {
                if ($str[$i] == '%' && $str[$i+1] == 'u')
                {
                        $val = hexdec(substr($str, $i+2, 4));

                        if ($val < 0x7f) $ret .= chr($val);
                        else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
                        else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));

                        $i += 5;
                }
                else if ($str[$i] == '%')
                {
                        $ret .= urldecode(substr($str, $i, 3));
                        $i += 2;
                }
                else $ret .= $str[$i];
        }
        return $ret;
}

function phpescape($str)
{
         $sublen=strlen($str);
         $retrunString="";
         for ($i=0;$i<$sublen;$i++)
         {
                  if(ord($str[$i])>=127)
                  {
                           $tmpString=bin2hex(iconv("gb2312","ucs-2",substr($str,$i,2)));
                           //$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);linux下打开这项
                           $retrunString.="%u".$tmpString;
                           $i++;
                  } else {
                           $retrunString.="%".dechex(ord($str[$i]));
                  }
         }
         return $retrunString;
}
$str=$_GET['str'];
$str=js_unescape($str);//这个是将unicode转成utf-8
$str= iconv('utf-8', 'gb2312', $str);//将utf-8转成gb2312


//这里自己加入判断
if($str=="root")$str="<font color=red>该用户名已存在</font>";
else $str="<font color=blue>该用户名可以使用</font>";
////结束自己加入判断的代码


echo phpescape($str);//返回到调用层

?>
运行a.php就可以看到效果了。 

回复帖子

内容:
抱歉,系统已内测完毕,本吧不能发帖
抱歉,系统已内测完毕,本吧不能发帖
用户名: 您目前是匿名发表
(快捷键:Ctrl+Enter)
 

本帖信息

点击数:202 回复数:4
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2010-10-17 22:15
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。