The Code
Here are few common ways to escape text for URLs in various scripting
languages.
- JavaScript
var artist = "Kruder & Dorfmeister";
artist = escape(artist);
- Perl
use URI::Escape;
my $artist = "Kruder & Dorfmeister";
$artist = uri_escape($artist);
- VBScript
strArtist = "Kruder & Dorfmeister"
strArtist = Server.URLEncode(strArtist)
- PHP
$artist = "Kruder & Dorfmeister";
$artist = urlencode(strArtist);
- Python
Unlike the previous examples, Python's
urlencode takes variable/value pairs and creates a
properly escaped querystring:
import sys
from urllib import urlencode
artist = "Kruder & Dorfmeister"
artist = urlencode({'ArtistSearch':artist})
This sets the variable artist equal to:
ArtistSearch=Kruder+%26+Dorfmeister
Encoding strings for URLs is an easy problem to solve, and it's
something to look at if your XML/HTTP requests aren't working
quite right.
eg.
http://mymachine/cgi-bin/check.cgi?code=abc#123
But it was only receiving first part of the value, before #
that is
code=abc
so i was very frustrated and
the problem was not getting solved.
When i got the result from this site,
use Time::Local;
my $code = uri_escape($code);
i am very happy now.
Thats great!!