|
目前共有5篇帖子。
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就可以看到效果了。
|
|
|