我有一个网址,想要解析其参数,例如:
function unescape (s)
s = string.gsub(s, "+", " ")
s = string.gsub(s, "%%(%x%x)", function (h)
return string.char(tonumber(h, 16))
end)
return s
end
function parseurl (s,param)
for k, v in string.gmatch( s, "([^&=?]+)=([^&=?]+)" ) do
--t[k] = v
if k == param then
--print (k.." "..v)
return unescape(v)
end
end
s = "http://www.page.com/link.php uname=Hans+Testmann&uemail=myemail%40gmail.com&utext=Now+this+is+working+great.%0D%0A++&mdt=1#&mydays:themeupload"s
比我打电话给它并获得结果,如之后 - >
parseurl (s, "uname") --> "Hans Testmann"
parseurl (s, "uemail") --> "myemail@gmail.com"
parseurl (s, "utext") --> "Now this is working great"
我已经解决了很多,似乎工作, 但你能看看它有可能改善吗?
答案 0 :(得分:3)
我会返回表中的所有参数并使用如下:
function urldecode(s)
s = s:gsub('+', ' ')
:gsub('%%(%x%x)', function(h)
return string.char(tonumber(h, 16))
end)
return s
end
function parseurl(s)
s = s:match('%s+(.+)')
local ans = {}
for k,v in s:gmatch('([^&=?]-)=([^&=?]+)' ) do
ans[ k ] = urldecode(v)
end
return ans
end
t = parseurl(s)
print(t.uname ) --> 'Hans Testmann'
print(t.uemail) --> 'myemail@gmail.com'
print(t.utext ) --> 'Now this is working great'