笔记 by 车东

Oneliner's url encode (urlencode) and url decode (urldecode) in Perl

· 技术笔记|Tech Notes

准备关键词测试时经常需要进行UrlEncode和UrlDecode,以下是2个常用的单行Perl脚本(正则表达式):输入为日志或关键词列表

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

💬 历史评论

以下 2 条评论从旧版 Movable Type 博客迁移而来。 原始评论时间:2002–2016 年。

Che Dong · 2004-09-14 15:06

使用iconv做编码转换: iconv -f -t input_file -f: from encoding -t: to encoding 比如: iconv -f utf-8 -t gb2312 wget.output

用来做UTF-8输出文件的编码转换挺方便的。这样在Linux上也能直接查看UTF-8的输出了。


我爱小甜饼 · 2006-05-24 16:39

在perl代码中需要将字符串转换为编码后的URL可以这么做: use URI::URL;

my $str = “http://www.google.com/?a=a b c&b=c d e”;

my $url = URI::URL->new( $str );

print $url;