3

在 Ruby 1.8 中,使用 URI 标准库,我可以解析

http://au.easyroommate.com/content/common/listing_detail.aspx?code=H123456789012&from=L123456789012345

用来URI.split得到

["http", nil, "au.easyroommate.com", nil, nil,
"/content/common/listing_detail.aspx", nil, 
"code=H123456789012&from=L123456789012345", nil]

但是是否可以在H123456789012不使用我自己的hackery的情况下从查询部分获取位(例如,拆分&然后获取匹配的位/code.(.*)/

4

2 回答 2

3

您正在寻找CGI::parse方法

params = CGI::parse("query_string")
  # {"name1" => ["value1", "value2", ...],
  #  "name2" => ["value1", "value2", ...], ... }
于 2011-07-30T02:23:56.313 回答
3

您可以使用Rack::Utilswhich 有一个方法parse_nested_query,您可以从 URL 中传递查询字符串:

Rack::Utils.parse_nested_query(uri.query_string)

然后,这将返回一个Hash表示查询字符串的对象,允许您访问code.

于 2011-07-30T02:24:20.523 回答