Use JavaScript to implement the urlencode and urldecode functions in PHP _ javascript skills

Source: Internet
Author: User
This article describes how to use JavaScript to implement the urlencode and urldecode functions of PHP. in many cases, we use the URL of phpurlencode, if you want to use the decodeURI function of JavaScript, you can refer to the decodeURI function of JavaScript. However, if you have any bugs, see the following functions. it's okay to use the function after testing, as I mentioned in previous articles, this is not the same as the urldecode function of php. The following is the urldecode function of the JavaScript version I have adapted based on the advanced code. the reference links are not listed in the articles mentioned at the beginning. Like the previous urlencode function, only the UTF-8 version is implemented.

1. urlencode

Usage: urlencode (str );

function urlencode(clearString) {var output = '';var x = 0;clearString = utf16to8(clearString.toString());var regex = /(^[a-zA-Z0-9-_.]*)/;while (x < clearString.length) {var match = regex.exec(clearString.substr(x));if (match != null && match.length > 1 && match[1] != '') {output += match[1];x += match[1].length;} else {if (clearString[x] == ' ')output += '+';else {var charCode = clearString.charCodeAt(x);var hexVal = charCode.toString(16);output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();}x++;}}function utf16to8(str) {var out, i, len, c;out = "";len = str.length;for(i = 0; i < len; i++) {c = str.charCodeAt(i);if ((c >= 0x0001) && (c <= 0x007F)) {out += str.charAt(i);} else if (c > 0x07FF) {out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));} else {out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));}}return out;}return output;}

2. urldecode

Usage: urldecode (url );

function urldecode(encodedString){var output = encodedString;var binVal, thisString;var myregexp = /(%[^%]{2})/;function utf8to16(str){var out, i, len, c;var char2, char3;out = "";len = str.length;i = 0;while(i < len) {c = str.charCodeAt(i++);switch(c >> 4){ case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:out += str.charAt(i-1);break;case 12: case 13:char2 = str.charCodeAt(i++);out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));break;case 14:char2 = str.charCodeAt(i++);char3 = str.charCodeAt(i++);out += String.fromCharCode(((c & 0x0F) << 12) |((char2 & 0x3F) << 6) |((char3 & 0x3F) << 0));break;}}return out;}while((match = myregexp.exec(output)) != null&& match.length > 1&& match[1] != ''){binVal = parseInt(match[1].substr(1),16);thisString = String.fromCharCode(binVal);output = output.replace(match[1], thisString);}//output = utf8to16(output);output = output.replace(/\\+/g, " ");output = utf8to16(output);return output;}

When the server uses php urlencode for transcoding, you can use js urldecode for parsing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.