How do I decode or create those %-encodings on the web?(contributed by brian d foy)
Those
In CGI scripts, you don't have to worry about decoding URIs if you are
using
If you have to encode a string yourself, remember that you should
never try to encode an already-composed URI. You need to escape the
components separately then put them together. To encode a string, you
can use the the my $original = "Colon : Hash # Percent %"; my $escaped = uri_escape( $original ); print "$escaped\n"; # 'Colon%20%3A%20Hash%20%23%20Percent%20%25'
To decode the string, use the my $unescaped = uri_unescape( $escaped ); print $unescaped; # back to original If you wanted to do it yourself, you simply need to replace the reserved characters with their encodings. A global substitution is one way to do it:
# encode
$string =~ s/([^^A-Za-z0-9\-_.!~*'()])/ sprintf "%%%0x", ord $1 /eg;
#decode
$string =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg;
|