urlencode() from PHP in JavaScript?
Solution 1
There is no function quite matching urlencode()
, but there is one quite equivalent to rawurlencode()
: encodeURIComponent()
.
Usage: var encoded = encodeURIComponent(str);
You can find a reference here:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
Solution 2
From: https://www.php.net/manual/en/function.urlencode.php
Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs
From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent:
encodeURIComponent() escapes all characters except:
Not Escaped: A-Z a-z 0-9 - _ . ! ~ * ' ( )
A snippet is provided near the bottom of that page which looks like this:
function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }
I am slightly adjusting the provided javascript snippet to include a couple more characters.
My Code:
function urlEncodeLikePHP(str) {
return encodeURIComponent(str).replace(/[.!~*'()]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
Usage:
urlEncodeLikePHP("._!_~_*_'_(_)-\\-&-|-/");
// effectively: "._!_~_*_'_(_)-\-&-|-/"
Encoded Output:
%2e_%21_%7e_%2a_%27_%28_%29-%5C-%26-%7C
Solution 3
Have a look at phpjs.org if you're searching for a JS function equivalent to PHP:
http://phpjs.org/functions/urlencode:573
Here you can use encodeURIComponent()
(with some modifications).
Solution 4
My code is the JS function equivalent to PHP's urlencode (based on PHP's source code).
function urlencode(str) {
let newStr = '';
const len = str.length;
for (let i = 0; i < len; i++) {
let c = str.charAt(i);
let code = str.charCodeAt(i);
// Spaces
if (c === ' ') {
newStr += '+';
}
// Non-alphanumeric characters except "-", "_", and "."
else if ((code < 48 && code !== 45 && code !== 46) ||
(code < 65 && code > 57) ||
(code > 90 && code < 97 && code !== 95) ||
(code > 122)) {
newStr += '%' + code.toString(16);
}
// Alphanumeric characters
else {
newStr += c;
}
}
return newStr;
}
daGrevis
Hacker working with Clojure, Python, ReactJS and Docker. Enjoys r/unixporn and r/vim. Loves DotA 2, blues rock and gym. Really loves his girlfriend. https://twitter.com/daGrevis https://github.com/daGrevis https://news.ycombinator.com/user?id=daGrevis https://lobste.rs/u/daGrevis http://www.linkedin.com/in/daGrevis
Updated on May 07, 2020Comments
-
daGrevis about 4 years
I'm looking for similar function as
urlencode()
from PHP just in JavaScript. jQuery library is allowed.Basically, I need to encode the string and then redirect the user to another page just with JavaScript.
-
mickmackusa over 4 yearsThis is not the same. One example is that
encodeURIComponent()
does not encode parentheses.