`
chenpeilei2003
  • 浏览: 187392 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

lua解析url参数

阅读更多
function Split(szFullString, szSeparator)
	local nFindStartIndex = 1
	local nSplitIndex = 1
	local nSplitArray = {}
	while true do
	   local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
	   if not nFindLastIndex then
	    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
	    break
	   end
	   nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
	   nFindStartIndex = nFindLastIndex + string.len(szSeparator)
	   nSplitIndex = nSplitIndex + 1
	end
	return nSplitArray
end

function parseUrl(url)
	local t1 = nil
	--,
	t1= Split(url,',')
	 
	--?
	url = t1[1]
	t1=Split(t1[1],'?')
	 
	url=t1[2]
	--&

	t1=Split(t1[2],'&')
	local res = {}
	for k,v in pairs(t1) do
		i = 1
		t1 = Split(v,'=')
		res[t1[1]]={}
		res[t1[1]]=t1[2]
		i=i+1
	end
	return res
end



--测试
url = 'http://192.168.1.113:8080/openG3/m/appPanel.vm?appId=1&appkey=111111&sid=12345,game/js/'

res = parseUrl('http://www.baidu.com')
print('appkey= '..res.appkey)
print('sid= '..res.sid)
print('appid= '..res.appId)
分享到:
评论
5 楼 chenpeilei2003 2013-07-09  
acmersch 写道
楼主对url中汉字或者特殊字符怎么解析?

汉字和其他特殊符号会先转换成UTF的百分号形式再发送。
4 楼 acmersch 2013-05-24  
楼主对url中汉字或者特殊字符怎么解析?
3 楼 瞎折腾球 2010-11-02  
需要添加kahlua库么
2 楼 chenpeilei2003 2010-10-31  
上面代码直接可以运行的
1 楼 瞎折腾球 2010-10-27  
朋友,能发给我这个demo么 !谢谢了156224301@qq.com

相关推荐

Global site tag (gtag.js) - Google Analytics