I wrote a custom xml parser and its locking up on special characters. So naturally I urlencoded them into my database.
I can't seem to find an equivalent to php's urldecode().
Are there any extentions for jquery or javascript that can accomplish this?
You could use the decodeURIComponent function to convert the %xx into characters. However, to convert + into spaces you need to replace them in an extra step.
function urldecode(url) {
return decodeURIComponent(url.replace(/\+/g, ' '));
}
Check out this one
function urldecode (str) {
return decodeURIComponent((str + '').replace(/\+/g, '%20'));
}
I think you need the decodeURI function.
Related
If I search for How to Format i get
How%20to%20Format
but I want to be How-to-Format
window.location = '/search/'+encodeURIComponent(query);
also escape(), encodeURI() dont work
Try like this:
window.location = '/search/'+ query.replace(/\s+/gi, '-');
Working example with stripping multi-spaces into one:
http://jsfiddle.net/VVEpE/
encodeURIComponent (and the broken, deprecated escape) will convert space characters to representations of a space that you can put in a URI.
If you want to use custom slug generation rules then you will have to write them yourself, probably using a regular expression.
e.g.
query.replace(/\s/g, "-");
string.replace(searchvalue,newvalue)
in this case
window.location = '/search/'+query.replace(/ /g, '-')
Try it out:
encodeURIComponent("'##$%^&");
If you try this out you will see all the special characters are encoded except for the single quote. What function can I use to encode ALL the characters and use PHP to decode them?
Thanks.
I'm not sure why you would want them to be encoded. If you only want to escape single quotes, you could use .replace(/'/g, "%27"). However, good references are:
When are you supposed to use escape instead of encodeURI / encodeURIComponent?
Comparing escape(), encodeURI(), and encodeURIComponent() at xkr.us
Javascript Madness: Query String Parsing #Javascript Encode/Decode Functions
You can use:
function fixedEncodeURIComponent (str) {
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}
fixedEncodeURIComponent("'##$%^&");
Check reference: http://mdn.beonex.com/en/JavaScript/Reference/Global_Objects/encodeURIComponent.html
You can use btoa() and atob(), this encodes and decodes the given string including single quote.
Just try encodeURI() and encodeURIComponent() yourself...
console.log(encodeURIComponent('##$%^&*'));
Input: ##$%^&*. Output: %40%23%24%25%5E%26*. So, wait, what happened to *? Why wasn't this converted? TLDR: You actually want fixedEncodeURIComponent() and fixedEncodeURI(). Long-story...
encodeURIComponent() : Do not use. Use fixedEncodeURIComponent(), as defined and explained by the MDN encodeURIComponent() Documentation, emphasis mine...
To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:
function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }
While we're on the topic, also don't use encodeURI(). MDN also has their own rewrite of it, as defined by the MDN encodeURI() Documentation. To quote their explanation...
If one wishes to follow the more recent RFC3986 for URLs, which makes square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following code snippet may help:
function fixedEncodeURI(str) { return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']'); }
Recent answer (2021)
Using JavaScript's URLSearchParams:
console.log(new URLSearchParams({ encoded: "'##$%^&" }).toString())
I found a neat trick that never misses any characters. I tell it to replace everything except for nothing. I do it like this (URL encoding):
function encode(w){return w.replace(/[^]/g,function(w){return '%'+w.charCodeAt(0).toString(16)})}
function encode(w){return w.replace(/[^]/g,function(w){return '%'+w.charCodeAt(0).toString(16)})}
loader.value = encode(document.body.innerHTML);
<textarea id=loader rows=11 cols=55>www.WHAK.com</textarea>
As #Bergi wrote, you can just replace all the characters:
function encoePicture(pictureUrl)
{
var map=
{
'&': '%26',
'<': '%3c',
'>': '%3e',
'"': '%22',
"'": '%27'
};
var encodedPic = encodeURI(pictureUrl);
var result = encodedPic.replace(/[&<>"']/g, function(m) { return map[m];});
return result;
}
I would like to encode my URL, but I want to convert spaces to plus symbols.
This is what I attempted to do...
var search = "Testing this here &";
encodeURIComponent(search.replace(/ /gi,"+"));
The output from that is Testing%2Bthis%2Bhere%2B%26 but what I would like it to be is Testing+this+here+%26 I tried replacing the space with %20 to convert it into a plus symbol, but that didn't seem to work. Can anyone tell me what it is I'm doing wrong here?
encodeURIComponent(search).replace(/%20/g, "+");
What you're doing wrong here is that first you convert spaces to pluses, but then encodeURIComponent converts pluses to "%2B".
Just try encodeURI() and encodeURIComponent() yourself...
console.log(encodeURIComponent('##$%^&*'));
Input: ##$%^&*. Output: %40%23%24%25%5E%26*. So, wait, what happened to *? Why wasn't this converted? TLDR: You actually want fixedEncodeURIComponent() and fixedEncodeURI(). Long-story...
Don't use encodeURIComponent() directly.
You should use fixedEncodeURIComponent(), as indicated by the MDN Documentation. encodeURIComponent does not encode any of the following: !',()*. You need to use this other function. It will solve not only your space problems, but other character problems.
function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }
To quote the MDN Documentation encodeURIComponent()...
To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used: fixedEncodeURIComponent().
I insert some strings in an array but before I do that I want to do what topic says. To only replace space with %20 I do:
Name.push(linkText.replace(" ", "%20"));
But how do I perform two "replace" there in one go?
Thanks in advance
It looks to me like you are trying to encode plaintext to use it in a URL or query string. I suspect you would be better off using one of javascript's built-in encoding methods, encodeURI or encodeURIComponent. See:
http://www.javascripter.net/faq/escape.htm
Do you want to replace two spaces in a row with one %20?
Name.push(linkText.replace(/ +/g, "%20"));
Or do you want to replace 2 spaces with %20%20?
Name.push(linkText.replace(/ /g, "%20"));
You could do something like this:
Name.push(linkText.replace(" ", "%20").replace("/", ""));
You can't do it using a single function call in regular JavaScript.
You could do this:
Name.push(linkText.replace(" ", "%20").replace("/", ""));
I'm using Google Translate API, and if I try to translate Mc Donald's this is what I get as a result:
Mc Donald's
How can I translate ' to ' -- in JavaScript -- and so on for any other numeric character reference?
Thanks!
Check those two:
Javascript equivalent for PHP's html_entity_decode
And its dependence:
Javascript equivalent for PHP's get_html_translation_table
JS libraries often have helper api's for this, Prototype for example has its unescapeHTML() function on String that works perfect, notice the examples:
http://prototypejs.org/api/string/unescapeHTML
Shouldn't something like this do it?
'Mc Donald's'.replace(/&#(\d+);/g, function(m, g) {
return String.fromCharCode(g);
});