Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kosh04
Created December 15, 2010 02:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosh04/741550 to your computer and use it in GitHub Desktop.
Save kosh04/741550 to your computer and use it in GitHub Desktop.
いろいろな言語でURLエンコード
#!/bin/bash
# 標準入力を URL エンコードする
# NOTE: urldecode は urlencode のシンボリックリンクとする
urlencode() {
# echo -n ${1-`cat`} | od -t x1 -A n | tr " " % | tr -d "\n"
# perl -pe 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg'
python -c 'import sys, urllib; print urllib.quote(sys.stdin.read()),'
# php -r 'echo urlencode(file_get_contents("php://stdin"))."\n";'
}
urldecode() {
# echo -e "$(sed -e 'y/+/ /; s/%\([[:xdigit:]]\{2\}\)/\\x\1/g')"
# perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2", $1))/eig'
python -c 'import sys, urllib; print urllib.unquote_plus(sys.stdin.read()),'
# php -r 'echo urldecode(file_get_contents("php://stdin"));'
}
if [ `basename $0` = "urldecode" ]
then
urldecode ${@+"$@"}
else
urlencode ${@+"$@"}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment