sub url_encode {
my $text = shift;
$text =~ s/([^a-z0-9_.!~*'() -])/sprintf "%%%02x", ord($1)/egi;
$text =~ tr/ /+/;
return $text;
}
sub url_decode {
my $text = shift;
$text =~ tr/\+/ /;
$text =~ s/%([a-f0-9][a-f0-9])/chr( hex( $1 ) )/egi;
return $text;
}
function urlencode(plaintext )
{
// the javascript escape and unescape functions do not correspond
// with what browsers actually do...
var safechars = "0123456789" + // numeric
"abcdefghijklmnopqrstuvwxyz" + // alphabetic
"abcdefghijklmnopqrstuvwxyz" +
"-_.!~*'()"; // rfc2396 mark characters
var hex = "0123456789abcdef";
var encoded = "";
for (var i = 0; i < plaintext.length; i++ ) {
var ch = plaintext.charat(i);
if (ch == " ") {
encoded += "+"; // x-www-urlencoded, rather than %20
} else if (safechars.indexof(ch) != -1) {
encoded += ch;
} else {
var charcode = ch.charcodeat(0);
if (charcode > 255) {
alert( "unicode character '"
+ ch
+ "' cannot be encoded using standard url encoding.\n" +
"(url encoding only supports 8-bit characters.)\n" +
"a space (+) will be substituted." );
encoded += "+";
} else {
encoded += "%";
encoded += hex.charat((charcode >> 4) & 0xf);
encoded += hex.charat(charcode & 0xf);
}
}
} // for
return encoded;
};