js网站源码已到期,高清品牌网站设计建设,申请域名空间,厦门seo网站排名优化本文介绍了php函数urlencode的js实现方法并比较js和php各编码函数的区别。 通常form表单的enctype类型为 application/x-www-form-urlencoded, 当表单提交后#xff0c;提交的数据自动被编码#xff0c; 规则为 除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两…本文介绍了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 varx0;4 clearStringclearString.toString();5 varregex/(^[a-zA-Z0-9-_.]*)/;6 while(x1match[1]!) {9 outputmatch[1];10 xmatch[1].length;11 }else{12 if(clearString.substr(x,1)) {13 //原文在此用 clearString[x] 做判断, 但ie不支持把字符串当作数组来访问,14 //修改后两种浏览器都可兼容15 output;16 }17 else{18 varcharCodeclearString.charCodeAt(x);19 varhexValcharCode.toString(16);20 output%( hexVal.length2?0:)hexVal.toUpperCase();21 }22 x;23 }24 }25 returnoutput;26 }注上面的代码引自 http://cass-hacks.com/articles/code/js_url_encode_decode/下面附上js和php几种编码方法对特殊符号的编码对照表InputJavaScriptPHPescapeencodeURIencodeURIComponenturlencoderawurlencodehtmlentities%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 stringstring.replace(/\r\n/g,\n);16 varutftext;17 18 for(varn0; n127)(c2048)) {26 utftextString.fromCharCode((c6)|192);27 utftextString.fromCharCode((c63)|128);28 }29 else{30 utftextString.fromCharCode((c12)|224);31 utftextString.fromCharCode(((c6)63)|128);32 utftextString.fromCharCode((c63)|128);33 }34 35 }36 37 returnutftext;38 },39 40 //private method for UTF-8 decoding41 _utf8_decode :function(utftext) {42 varstring;43 vari0;44 varcc1c20;45 46 while( i191)(c224)) {55 c2utftext.charCodeAt(i1);56 stringString.fromCharCode(((c31)6)|(c263));57 i2;58 }59 else{60 c2utftext.charCodeAt(i1);61 c3utftext.charCodeAt(i2);62 stringString.fromCharCode(((c15)12)|((c263)6)|(c363));63 i3;64 }65 66 }67 68 returnstring;69 }70 71 }