php中escape unescape的写法

  1. */
  2. function phpescape($str){
  3. $sublen=strlen($str);
  4. $restring="";
  5. for ($i=0;$i<$sublen;$i++){
  6. if(ord($str[$i])>=127){
  7. $tmps教程tring=bin2hex(iconv("utf-8","ucs-2",substr($str,$i,2)));
  8. if (!eregi("win",php_os)){
  9. $tmpstring=substr($tmpstring,2,2).substr($tmpstring,0,2);
  10. }
  11. $restring.="%u".$tmpstring;
  12. $i++;
  13. } else {
  14. $restring.="%".dechex(ord($str[$i]));
  15. }
  16. }
  17. return $restring;
  18. }
  19. function unescape($str) {
  20. $str = rawurldecode($str);
  21. preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/u",$str,$r);
  22. $ar = $r[0];
  23. foreach($ar as $k=>$v) {
  24. if(substr($v,0,2) == "%u")
  25. $ar[$k] = iconv("ucs-2","utf-8",pack("h4",substr($v,-4)));
  26. elseif(substr($v,0,3) == "&#x")
  27. $ar[$k] = iconv("ucs-2","utf-8",pack("h4",substr($v,3,-1)));
  28. elseif(substr($v,0,2) == "&#") {
  29. $ar[$k] = iconv("ucs-2","utf-8",pack("n",substr($v,2,-1)));
  30. }
  31. }
  32. return join("",$ar);
  33. }
  34. function escape($str) {
  35. preg_match_all("/[x80-xff].|[x01-x7f]+/",$str,$r);
  36. $ar = $r[0];
  37. foreach($ar as $k=>$v) {
  38. if(ord($v[0]) < 128)
  39. $ar[$k] = rawurlencode($v);
  40. else
  41. $ar[$k] = "%u".bin2hex(iconv("gb2312","ucs-2",$v));
  42. }
  43. return join("",$ar);
  44. }
  45. function unescape($str) {
  46. $str = rawurldecode($str);
  47. preg_match_all("/(?:%u.{4})|.+/",$str,$r);
  48. $ar = $r[0];
  49. foreach($ar as $k=>$v) {
  50. if(substr($v,0,2) == "%u" && strlen($v) == 6)
  51. $ar[$k] = iconv("ucs-2","gb2312",pack("h4",substr($v,-4)));
  52. } //开源代码phpfensi.com
  53. return join("",$ar);
  54. }