5

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.

1
  • Favorited. This is something I want to do once every six months or so, and I always end up re-rolling something to sort of get the job done. – Pillsy Jul 2 '10 at 14:14
6

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.

1
  • 1
    Both answers are worthy of upvotes, but if it were my question I'd accept this one, because there's a lot of useful stuff you can get via JLink that I always plain forget about. – Pillsy Jul 2 '10 at 14:15
4

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
unesc[s_String] := subst[s, re@"\\%(..)":>FromCharacterCode@FromDigits["$1",16]]

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, "&"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.