js urlencode 20 php,js实现php函数urlencode
本文介紹了php函數(shù)urlencode的js實(shí)現(xiàn)方法并比較js和php各編碼函數(shù)的區(qū)別。 通常form表單的enctype類型為 application/x-www-form-urlencoded, 當(dāng)表單提交后,提交的數(shù)據(jù)自動(dòng)被編碼, 規(guī)則為 除了 -_. 之外的所有非字母數(shù)字字符都將被替換成百分號(hào)(%)后跟兩
本文介紹了php函數(shù)urlencode的js實(shí)現(xiàn)方法并比較js和php各編碼函數(shù)的區(qū)別。
通常form表單的enctype類型為 application/x-www-form-urlencoded, 當(dāng)表單提交后,提交的數(shù)據(jù)自動(dòng)被編碼, 規(guī)則為" 除了 -_. 之外的所有非字母數(shù)字字符都將被替換成百分號(hào)(%)后跟兩位十六進(jìn)制數(shù),空格則編碼為加號(hào)(+)。", php的urlencode函數(shù)與其功能相同。
js編碼方法:escape, encodeURI, encodeURIComponent。
escape可以對(duì)大多數(shù)符號(hào)進(jìn)行編碼,但是對(duì)unicode字符無(wú)效。
php編碼方法:urlencode, rawurlencode, htmlentities。
urlencode和rawurlencode唯一的區(qū)別是對(duì)空格的編碼方式不同,rawurlencode遵循RFC 1738編碼將空格轉(zhuǎn)換為 %20。
如何用js實(shí)現(xiàn)php的urlencode功能, 網(wǎng)上流傳著一段js和vbscript混寫的代碼,通用性不好,另找到國(guó)外一高人寫的, 經(jīng)測(cè)試與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不支持把字符串當(dāng)作數(shù)組來(lái)訪問,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幾種編碼方法對(duì)特殊符號(hào)的編碼對(duì)照表:
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/
另一個(gè)非常優(yōu)秀的urlencode和urldecode函數(shù)
代碼
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?}
總結(jié)
以上是生活随笔為你收集整理的js urlencode 20 php,js实现php函数urlencode的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue render函数
- 下一篇: npy文件的处理方式