Perl笔记

Monday, August 28, 2006

url encode and url decode in Perl

url-encode:
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
url-decode:
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;

Onliner's:
  • Urlencode:对 \n 不转码
    perl -p -e 's/([^\w\-\.\@])/$1 eq "\n" ? "\n":sprintf("%%%2.2x",ord($1))/eg' keywords.list
  • UrlDecode:
    perl -p -e 's/%(..)/pack("c", hex($1))/eg' query.log

0 Comments:

Post a Comment

<< Home