日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

js urlencode 20 php,js实现php函数urlencode

發布時間:2023/12/10 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js urlencode 20 php,js实现php函数urlencode 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文介紹了php函數urlencode的js實現方法并比較js和php各編碼函數的區別。 通常form表單的enctype類型為 application/x-www-form-urlencoded, 當表單提交后,提交的數據自動被編碼, 規則為 除了 -_. 之外的所有非字母數字字符都將被替換成百分號(%)后跟兩

本文介紹了php函數urlencode的js實現方法并比較js和php各編碼函數的區別。

通常form表單的enctype類型為 application/x-www-form-urlencoded, 當表單提交后,提交的數據自動被編碼, 規則為" 除了 -_. 之外的所有非字母數字字符都將被替換成百分號(%)后跟兩位十六進制數,空格則編碼為加號(+)。", php的urlencode函數與其功能相同。

js編碼方法:escape, encodeURI, encodeURIComponent。

escape可以對大多數符號進行編碼,但是對unicode字符無效。

php編碼方法:urlencode, rawurlencode, htmlentities。

urlencode和rawurlencode唯一的區別是對空格的編碼方式不同,rawurlencode遵循RFC 1738編碼將空格轉換為 %20。

如何用js實現php的urlencode功能, 網上流傳著一段js和vbscript混寫的代碼,通用性不好,另找到國外一高人寫的, 經測試與urlencode相同。

代碼

1?functionURLEncode?(clearString)?{2?varoutput='';3?varx=0;4?clearString=clearString.toString();5?varregex=/(^[a-zA-Z0-9-_.]*)/;6?while(x1&&match[1]!='')?{9?output+=match[1];10?x+=match[1].length;11?}else{12?if(clearString.substr(x,1)=='')?{13?//原文在此用?clearString[x]?==?'?'?做判斷,?但ie不支持把字符串當作數組來訪問,14?//修改后兩種瀏覽器都可兼容15?output+='+';16?}17?else{18?varcharCode=clearString.charCodeAt(x);19?varhexVal=charCode.toString(16);20?output+='%'+(?hexVal.length<2?'0':'')+hexVal.toUpperCase();21?}22?x++;23?}24?}25?returnoutput;26?}

注:上面的代碼引自?http://cass-hacks.com/articles/code/js_url_encode_decode/

下面附上js和php幾種編碼方法對特殊符號的編碼對照表:

Input

JavaScript

PHP

escape

encodeURI

encodeURIComponent

urlencode

rawurlencode

htmlentities

%20

%20

%20

+

%20

!

%21

!

!

%21

%21

!

@

@

@

%40

%40

%40

@

#

%23

#

%23

%23

%23

#

$

%24

$

%24

%24

%24

$

%

%25

%25

%25

%25

%25

%

^

%5E

%5E

%5E

%5E

%5E

^

&

%26

&

%26

%26

%26

&

*

*

*

*

%2A

%2A

*

(

%28

(

(

%28

%28

(

)

%29

)

)

%29

%29

)

-

-

-

-

-

-

-

_

_

_

_

_

_

_

=

%3D

=

%3D

%3D

%3D

=

+

+

+

%2B

%2B

%2B

+

:

%3A

:

%3A

%3A

%3A

:

;

%3B

;

%3B

%3B

%3B;

;

.

.

.

.

.

.

.

"

%22

%22

%22

%22

%22

"

'

%27

'

'

%27

%27

'

\

%5C

%5C

%5C

%5C

%5C

\

/

/

/

%2F

%2F

%2F

/

?

%3F

?

%3F

%3F

%3F

?

<

%3C

%3C

%3C

%3C

%3C

<

>

%3E

%3E

%3E

%3E

%3E

>

~

%7E

~

~

%7E

%7E

~

[

%5B

%5B

%5B

%5B

%5B

[

]

%5D

%5D

%5D

%5D

%5D

]

{

%7B

%7B

%7B

%7B

%7B

{

}

%7D

%7D

%7D

%7D

%7D

}

`

%60

%60

%60

%60

%60

`

上表引自?http://www.the-art-of-web.com/javascript/escape/

另一個非常優秀的urlencode和urldecode函數

代碼

1?varUrl={2?3?//public?method?for?url?encoding4?encode?:function(string)?{5?returnescape(this._utf8_encode(string));6?},7?8?//public?method?for?url?decoding9?decode?:function(string)?{10?returnthis._utf8_decode(unescape(string));11?},12?13?//private?method?for?UTF-8?encoding14?_utf8_encode?:function(string)?{15?string=string.replace(/\r\n/g,"\n");16?varutftext="";17?18?for(varn=0;?n127)&&(c<2048))?{26?utftext+=String.fromCharCode((c>>6)|192);27?utftext+=String.fromCharCode((c&63)|128);28?}29?else{30?utftext+=String.fromCharCode((c>>12)|224);31?utftext+=String.fromCharCode(((c>>6)&63)|128);32?utftext+=String.fromCharCode((c&63)|128);33?}34?35?}36?37?returnutftext;38?},39?40?//private?method?for?UTF-8?decoding41?_utf8_decode?:function(utftext)?{42?varstring="";43?vari=0;44?varc=c1=c2=0;45?46?while(?i191)&&(c<224))?{55?c2=utftext.charCodeAt(i+1);56?string+=String.fromCharCode(((c&31)<<6)|(c2&63));57?i+=2;58?}59?else{60?c2=utftext.charCodeAt(i+1);61?c3=utftext.charCodeAt(i+2);62?string+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));63?i+=3;64?}65?66?}67?68?returnstring;69?}70?71?}

總結

以上是生活随笔為你收集整理的js urlencode 20 php,js实现php函数urlencode的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。