Skip to content

Instantly share code, notes, and snippets.

@CaptainLiao
Created December 29, 2018 04:30
Show Gist options
  • Save CaptainLiao/de0ff0b78123b894d537764da3ecb105 to your computer and use it in GitHub Desktop.
Save CaptainLiao/de0ff0b78123b894d537764da3ecb105 to your computer and use it in GitHub Desktop.
在 bash 中实现 urlencode,转义中文字符
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