upwithabang web designer resources

Donation? Yes, it is better than ads and it's optional. If this site has helped you please consider remunerating an amount you deem appropriate.


del.icio.us icon

Escaping in a Query String

As you are probably aware HTML requires that the ampersade character & be typed out as &. This must also be done in URLs:

The address:

script.pl?task=1&category=happiness

becomes:

script.pl?task=1&category=happiness

URL contains an ampersade

What happens when the query string contains a ampersade? For example:

script.pl?task=1&category=bits & bytes

In the example above the category is "bits & bytes" but since query strings recognize the ampersade as a field delimitor we have a problem. HTTP (which the descibes how a URL should look) has solution. The ampersade that is not a delimitor in the query string should be escaped into URL encoding. As, incidently, should spaces. An ampersade can be represented in a URL as %26. A space as %20. Therefore, the solution to the problem above is:

script.pl?task=1&category=bits%20%26%20bytes

The anchor might look like this:

<a href='script.pl?task=1&category=bits%20%26%20bytes'>Show Category</a>

The follow table shows useful escape codes for use in URL query string.

Normal Character Encoding Character
 (space) %20
& %26
' (single quote) %27
" %22
? %3F
= %3D
+ %2B
% %25

Did you find this article userful for your website?
If so, why not provide a link to it? Just copy the following to your web page:
      <a href='http://upwithabang.com/articles/html-query-string-escapes.html'>
Escaping query strings
</a>

Share this:  del.icio.us icon