Created
December 29, 2018 04:30
-
-
Save CaptainLiao/de0ff0b78123b894d537764da3ecb105 to your computer and use it in GitHub Desktop.
在 bash 中实现 urlencode,转义中文字符
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urlencode() { | |
local length="${#1}" | |
for (( i = 0; i < length; i++ )); do | |
local c="${1:i:1}" | |
case $c in | |
[a-zA-Z0-9.~_-]) printf "$c" ;; | |
*) printf "$c" | xxd -p -c1 | while read x;do printf "%%%s" "$x";done | |
esac | |
done | |
} | |
# 引用 https://gist.github.com/cdown/1163649 | |
# 使用方法 | |
# echo urlencode 你好世界 | |
# 输出 | |
# %e4%bd%a0%e5%a5%bd%e4%b8%96%e7%95%8c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment