LoginSignup
2
2

More than 5 years have passed since last update.

URLエンコード/デコード

Last updated at Posted at 2018-02-23

表題のとおり urlエンコード/デコードしたい
linux で パイプやリダイレクトで利用したい

nkf を利用する方法がいくつか試みたが
どうも長い文字列などで文字化けするようだ

こんな時は perlが頼り

urlencode.pl
#!/usr/bin/perl

while(<>){
  $str .= $_;
}
$str =~ s/([^ 0-9a-zA-Z])/"%".uc(unpack("H2",$1))/eg;
$str =~ s/ /+/g;
print($str);

urldecode.pl
#!/usr/bin/perl

while(<>){
  $str .= $_;
}
$str =~ s/\+/ /g;
$str =~ s/%([0-9a-fA-F]{2})/pack("H2",$1)/eg;
print($str);

動作チェック

青空文庫「走れメロス」をエンコードしてデコード
比較する

curl -s http://www.aozora.gr.jp/cards/000035/files/1567_14913.html | nkf -w > sample.txt
urlencode.pl sample.txt | urldecode.pl  > sample.out
cmp sample.txt sample.out 

参考にしたページ
- nkfでURLエンコード
- URLエンコード / URLデコードするには (urlencode, urldecode)

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2