rami.info



URLEncode And URLDecode For Perl

Posted in Code, Perl by Rami on the November 19th, 2005

PHP has builtin functions for urlencode and urldecode functions but for perl I’ve found the following from HERE that does the same thing:

sub URLDecode {
my $theURL = $_[0];
$theURL =~ tr/+/ /;
$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$theURL =~ s/<!–(.|\n)*–>//g;
return $theURL;
}

sub URLEncode {
my $theURL = $_[0];
$theURL =~ s/([\W])/”%” . uc(sprintf(”%2.2x”,ord($1)))/eg;
return $theURL;
}