For example,
urlesc["foo.cgi?abc=123"]
should return
foo.cgi%3Fabc%3D123
This is also known as percent-encoding.
Also, for better readability, spaces should encode to pluses. I believe that's always acceptable for URL escaping.
For example,
urlesc["foo.cgi?abc=123"]
should return
foo.cgi%3Fabc%3D123
This is also known as percent-encoding.
Also, for better readability, spaces should encode to pluses. I believe that's always acceptable for URL escaping.
Here's my solution:
cat = StringJoin@@(ToString/@{##})&; (* Like sprintf/strout in C/C++. *)
re = RegularExpression;
hex = IntegerString[#,16]&; (* integer to hex, represented as a string *)
up = ToUpperCase;
asc = ToCharacterCode[#][[1]]&; (* character to ascii code *)
subst = StringReplace;
urlesc[s_String] := subst[s, {" "->"+", re@"[^\w\_\:\.]":>"%"<>up@hex@asc@"$0"}]
urlesc[x_] := urlesc@cat@x
As a bonus, here's a function to encode a list of rules like {a->2, b->3}
into GET parameters like a=2&b=3
, with appropriate URL-encoding:
encode[c_] := cat @@ Riffle[cat[#1, "=", urlesc[#2]]& @@@ c, "&"]
Another method, using J/Link and java.net.URLEncoder
:
In[116]:= Needs["JLink`"]; InstallJava[];
LoadJavaClass["java.net.URLEncoder"];
In[118]:= URLEncoder`encode["foo.cgi?abc=123"]
Out[118]= "foo.cgi%3Fabc%3D123"
There's also java.net.URLDecoder
for decoding.