PHP實現Javascript中的escape及unescape函數代碼分享_PHP教程

來源:互聯網
上載者:User

PHP實現Javascript中的escape及unescape函數代碼分享


這篇文章主要介紹了PHP實現Javascript中的escape及unescape函數代碼分享,本文給出兩個實現版本,需要的朋友可以參考下

這個類相當好用.作用麼,PHP做JSON傳遞GBK字元,比如中文,日文,韓文神馬的Unicode最合適不過了..

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

classcoding

{

//模仿JAVASCRIPT的ESCAPE和UNESCAPE函數的功能

functionunescape($str)

{

$text=preg_replace_callback("/%u[0-9A-Za-z]{4}/",array(

&$this,

'toUtf8'

),$str);

returnmb_convert_encoding($text,"gb2312","utf-8");

}

functiontoUtf8($ar)

{

foreach($aras$val){

$val=intval(substr($val,2),16);

if($val<0x7F){// 0000-007F

$c.=chr($val);

}elseif($val<0x800){// 0080-0800

$c.=chr(0xC0|($val/64));

$c.=chr(0x80|($val%64));

}else{// 0800-FFFF

$c.=chr(0xE0|(($val/64)/64));

$c.=chr(0x80|(($val/64)%64));

$c.=chr(0x80|($val%64));

}

}

return$c;

}

functionescape($string,$encoding='gb2312')

{

$return='';

for($x=0;$x

$str=mb_substr($string,$x,1,$encoding);

if(strlen($str)>1){// 多位元組字元

$return.='%u'.strtoupper(bin2hex(mb_convert_encoding($str,'UCS-2',$encoding)));

}else{

$return.='%'.strtoupper(bin2hex($str));

}

}

return$return;

}

functiongb2utf8($string,$encoding='utf-8',$from_encode='gb2312')

{

returnmb_convert_encoding($string,$encoding,$from_encode);

}

}

?>

google code 上找到的另外一個類似指令碼

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

functionphpescape($str)

{

$sublen=strlen($str);

$retrunString="";

for($i=0;$i<$sublen;$i++)

{

if(ord($str[$i])>=127)

{

$tmpString=bin2hex(iconv("gbk","ucs-2",substr($str,$i,2)));

$tmpString=substr($tmpString,2,2).substr($tmpString,0,2);

$retrunString.="%u".$tmpString;

$i++;

}else{

$retrunString.="%".dechex(ord($str[$i]));

}

}

return$retrunString;

}

functionescape($str)

{

preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);

$ar=$r[0];

foreach($aras$k=>$v)

{

if(ord($v[0])<128)

$ar[$k]=rawurlencode($v);

else

$ar[$k]="%u".bin2hex(iconv("UTF-8","UCS-2",$v));

}

returnjoin("",$ar);

}

functionphpunescape($source)

{

$decodedStr="";

$pos=0;

$len=strlen($source);

while($pos<$len)

{

$charAt=substr($source,$pos,1);

if($charAt=='%')

{

$pos++;

$charAt=substr($source,$pos,1);

if($charAt=='u')

{

// we got a unicode character

$pos++;

$unicodeHexVal=substr($source,$pos,4);

$unicode=hexdec($unicodeHexVal);

$entity="&#".$unicode.';';

$decodedStr.=utf8_encode($entity);

$pos+=4;

}else{

// we have an escaped ascii character

$hexVal=substr($source,$pos,2);

$decodedStr.=chr(hexdec($hexVal));

$pos+=2;

}

}else{

$decodedStr.=$charAt;

$pos++;

}

}

return$decodedStr;

}

functionunescape($str)

{

$str=rawurldecode($str);

preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);

$ar=$r[0];

#print_r($ar);

foreach($aras$k=>$v)

{

if(substr($v,0,2)=="%u")

$ar[$k]=iconv("UCS-2","UTF-8",pack("H4",substr($v,-4)));

elseif(substr($v,0,3)=="&#x")

$ar[$k]=iconv("UCS-2","UTF-8",pack("H4",substr($v,3,-1)));

elseif(substr($v,0,2)=="&#")

{

//echo substr($v,2,-1)."";

$ar[$k]=iconv("UCS-2","UTF-8",pack("n",substr($v,2,-1)));

}

}

returnjoin("",$ar);

}

?>

http://www.bkjia.com/PHPjc/955836.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/955836.htmlTechArticlePHP實現Javascript中的escape及unescape函數代碼分享 這篇文章主要介紹了PHP實現Javascript中的escape及unescape函數代碼分享,本文給出兩個實現版本,需...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.