用JS解碼PHP的urlencode編碼
- JS的編碼、解碼方法裏,decodeURI和PHP的urlencode方法不一樣,沒法對PHP的urlencode進行解碼。
- function URLdecode(str) {
- var ret = "";
- for(var i=0;i<str.length;i++) {
- var chr = str.charAt(i);
- if(chr == "+") {
- ret += " ";
- }else if(chr=="%") {
- var asc = str.substring(i+1,i+3);
- if(parseInt("0x"+asc)>0x7f) {
- ret += decodeURI("%"+ str.substring(i+1,i+9));
- i += 8;
- }else {
- ret += String.fromCharCode(parseInt("0x"+asc));
- i += 2;
- }
- }else {
- ret += chr;
- }
- }
- return ret;
- }
- alert(URLdecode("<?php echo $test_1 ?>"));
歡迎關注本站公眾號,獲取更多信息