function escape($str) {
//echo $str;
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
$ar = $r[0];foreach($ar as $k=>$v) {
if(ord($v) < 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));}
return join("",$ar);
}

解决方案 »

  1.   

    http://topic.csdn.net/t/20051212/09/4452952.html
      

  2.   

    /**************escape*********************/chr
    (PHP 3, PHP 4, PHP 5)
    chr -- 返回指定的字符
    描述
    string chr ( int ascii )
    返回相对应于 ascii 所指定的单个字符。 例子 1. chr() 示例<?php
    $str = "The string ends in escape: ";
    $str .= chr(27); /* 在 $str 后边增加换码符 *//* 通常这样更有用 */$str = sprintf("The string ends in escape: %c", 27);
    ?> 
    你可以在此处找到 ASCII 码表:http://www.asciitable.com。 
    此函数与 ord() 是互补的。/*************unescape*****************/ord
    (PHP 3, PHP 4, PHP 5)ord -- Return ASCII value of character
    Description
    int ord ( string string )
    Returns the ASCII value of the first character of string. This function complements chr(). 例子 1. ord() example<?php
    $str = "\n";
    if (ord($str) == 10) {
        echo "The first character of \$str is a line feed.\n";
    }
    ?>
      

  3.   

    这个我一直在用肯定行的
    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); 
    }