-- 分割字符串 localfunctionsplit(str, s) local result={} string.gsub(str,'[^'..s..']+',function(w) table.insert(result, w) end) return result end
-- 中文转 localfunctionencodeUrl(s) s = string.gsub(s, "([^%w%.%- ])", function(c) returnstring.format("%%%02X", string.byte(c)) end) returnstring.gsub(s, " ", "+") end
localfunctionparseURL(url) print(url) local pre = split(url, "?") if #pre==1thenreturn url end local new_url = pre[1].."?" local arr = split(pre[2], "&") for i = 1, #arr do local temp = split(arr[i], "=") new_url = new_url..temp[1].."="..encodeUrl(temp[2]) if i < #arr then new_url = new_url.."&"end end return new_url end
local url = "https://www.baidu.com/s?wd=你好" local new_url = parseURL(url) print(new_url)