UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

25 lines (24 loc) 1.39 kB
function rawurldecode(str) { // discuss at: http://phpjs.org/functions/rawurldecode/ // original by: Brett Zamir (http://brett-zamir.me) // input by: travc // input by: Brett Zamir (http://brett-zamir.me) // input by: Ratheous // input by: lovio // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // reimplemented by: Brett Zamir (http://brett-zamir.me) // improved by: Brett Zamir (http://brett-zamir.me) // note: Please be aware that this function expects to decode from UTF-8 encoded strings, as found on // note: pages served as UTF-8 // example 1: rawurldecode('Kevin+van+Zonneveld%21'); // returns 1: 'Kevin+van+Zonneveld!' // example 2: rawurldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F'); // returns 2: 'http://kevin.vanzonneveld.net/' // example 3: rawurldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'); // returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a' return decodeURIComponent((str + '') .replace(/%(?![\da-f]{2})/gi, function() { // PHP tolerates poorly formed escape sequences return '%25'; })); }