假设我有一个字符串,其中包含params:
http://www.google.com/search?hl=en&client=firefox-a&hs=zlX&rls=org.mozilla%3Aen-US%3Aofficial&q=something&cts=1279154269301&aq=f&aqi=&aql=&oq=&gs_rfai=
如何从字符串中形成所有参数的数组?我知道你可以访问的params
数组,但我所说的只是任意字符串,而不是请求的一部分。
答案 0 :(得分:2)
不确定是否有导轨快捷方式,但是:
url = 'http://www.google.com/search?hl=en&client=firefox-a&hs=zlX&rls=org.mozilla%3Aen-US%3Aofficial&q=something&cts=1279154269301&aq=f&aqi=&aql=&oq=&gs_rfai='
params = CGI.parse(URI.parse(url).query)
#=> {"hs"=>["zlX"], "oq"=>[""], "cts"=>["1279154269301"], "aqi"=>[""], "rls"=>["org.mozilla:en-US:official"], "hl"=>["en"], "aq"=>["f"], "gs_rfai"=>[""], "aql"=>[""], "q"=>["something"], "client"=>["firefox-a"]}
params = CGI.parse(url)
几乎就是给你:
#=> {"hs"=>["zlX"], "oq"=>[""], "cts"=>["1279154269301"], "aqi"=>[""], "rls"=>["org.mozilla:en-US:official"], "aq"=>["f"], "http://www.google.com/search?hl"=>["en"], "gs_rfai"=>[""], "aql"=>[""], "q"=>["something"], "client"=>["firefox-a"]}