개요
- urlencode
- urllib.quote_plus
- urldecode()의 역함수
- URL을 쿼리스트링으로 넘길 수 있도록 변환 가능
- 이 목적이라면 함수 rawurlencode()를 사용해도 됨
| 입력값 | 출력값 |
|---|---|
(space) |
+
|
/ |
%2F
|
% |
%25
|
Bash
- with Python
input='hello 123 http://zetawiki.com 한글'
output=`python -c "import urllib; print urllib.quote_plus('''$input''')"`
echo $output
# hello+123+http%3A%2F%2Fzetawiki.com+%ED%95%9C%EA%B8%80
- with PHP
input='hello 123 http://zetawiki.com 한글'
output=`php -r "echo urlencode('$input');"`
echo $output
# hello+123+http%3A%2F%2Fzetawiki.com+%ED%95%9C%EA%B8%80
Java
String input = "hello 123 http://zetawiki.com 한글";
String encoded = URLEncoder.encode(input, "UTF-8");
System.out.println(encoded);
// hello+123+http%3A%2F%2Fzetawiki.com+%ED%95%9C%EA%B8%80
JavaScript
function urlencode(str) {
return encodeURIComponent(str+'').replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
PHP
echo urlencode('개요');
# %EA%B0%9C%EC%9A%94
echo urlencode('hello 123 http://zetawiki.com 한글');
# hello+123+http%3A%2F%2Fzetawiki.com+%ED%95%9C%EA%B8%80
Python
Python 2
# -*- coding: utf-8 -*-
import urllib
print urllib.quote_plus('hello 123 http://zetawiki.com 한글')
# hello+123+http%3A%2F%2Fzetawiki.com+%ED%95%9C%EA%B8%80
R
# Windows (CP949) 예시
s <- 'hello 123 http://zetawiki.com 한글'
gsub('%20','+',URLencode(iconv(s,'CP949','UTF-8'),reserved=T))
## [1] "hello+123+http%3A%2F%2Fzetawiki.com+%ED%95%9C%EA%B8%80"