How can I “URL decode” a string in Emacs Lisp?
url-unhex-string
url-unhex-string
There is a simple way to konw if a URL string is encoded. Take the initial string and compare it with the result of decoding it. If the result is the same, the string is not encoded; if the result is different then it is encoded. I had this issue with my urls and I … Read more
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, ‘ ‘)); }
To encode and decode urls create this extention somewhere in the project: Swift 2.0 extension String { func encodeUrl() -> String { return self.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet()) } func decodeUrl() -> String { return self.stringByRemovingPercentEncoding } } Swift 3.0 extension String { func encodeUrl() -> String { return self.addingPercentEncoding( withAllowedCharacters: NSCharacterSet.urlQueryAllowed()) } func decodeUrl() -> String { … Read more
Here is a simple one-line solution. $ function urldecode() { : “${*//+/ }”; echo -e “${_//%/\\x}”; } It may look like perl 🙂 but it is just pure bash. No awks, no seds … no overheads. Using the : builtin, special parameters, pattern substitution and the echo builtin’s -e option to translate hex codes into … Read more
I faced the encoding half of this problem the other day. Unhappy with the available options, and after taking a look at this C sample code, i decided to roll my own C++ url-encode function: #include <cctype> #include <iomanip> #include <sstream> #include <string> using namespace std; string url_encode(const string &value) { ostringstream escaped; escaped.fill(‘0’); escaped … Read more
Edit (2017-10-12): @MechaLynx and @Kevin-Weber note that unescape() is deprecated from non-browser environments and does not exist in TypeScript. decodeURIComponent is a drop-in replacement. For broader compatibility, use the below instead: decodeURIComponent(JSON.parse(‘”http\\u00253A\\u00252F\\u00252Fexample.com”‘)); > ‘http://example.com’ Original answer: unescape(JSON.parse(‘”http\\u00253A\\u00252F\\u00252Fexample.com”‘)); > ‘http://example.com’ You can offload all the work to JSON.parse
Here is a complete function (taken from PHPJS): function urldecode(str) { return decodeURIComponent((str+”).replace(/\+/g, ‘%20’)); }
string decodedUrl = Uri.UnescapeDataString(url) or string decodedUrl = HttpUtility.UrlDecode(url) Url is not fully decoded with one call. To fully decode you can call one of this methods in a loop: private static string DecodeUrlString(string url) { string newUrl; while ((newUrl = Uri.UnescapeDataString(url)) != url) url = newUrl; return newUrl; }