I have some URLs, like
我有一些url,比如
http://www.example.com/something?param1=value1¶m2=value2¶m3=value3
and I would like to extract the parameters from these URLs and get them in a Hash. Obviously, I could use regular expressions, but I was just wondering if there was easier ways to do that with Ruby or Rails. I haven't found anything in the Ruby module URI
but perhaps I missed something.
我想从这些url中提取参数,并将它们转换成散列。显然,我可以使用正则表达式,但是我想知道是否有更简单的方法来使用Ruby或Rails。我在Ruby模块URI中没有找到任何东西,但可能漏掉了什么。
In fact, I need a method that would do that:
事实上,我需要一种能够做到这一点的方法:
extract_parameters_from_url("http://www.example.com/something?param1=value1¶m2=value2¶m3=value3")
#=> {:param1 => 'value1', :param2 => 'value2', :param3 => 'value3'}
Would you have some advices?
你有什么建议吗?
7 个解决方案
#1
137
I think you want to turn any given URL string into a HASH?
我想你想把任何给定的URL字符串转换成散列?
You can try http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075
你可以试试http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html M000075
require 'cgi'
CGI::parse('param1=value1¶m2=value2¶m3=value3')
returns
返回
{"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}
#2
137
I found myself needing the same thing for a recent project. Building on Levi's solution, here's a cleaner and faster method:
我发现自己在最近的一个项目中也需要同样的东西。基于Levi's的解决方案,这里有一个更干净、更快的方法:
Rack::Utils.parse_nested_query 'param1=value1¶m2=value2¶m3=value3'
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}
#3
85
Just Improved with Levi answer above -
刚刚改进了以上的Levi回答
Rack::Utils.parse_query URI("http://example.com?par=hello&par2=bye").query
For a string like above url, it will return
对于像上面url这样的字符串,它将返回
{ "par" => "hello", "par2" => "bye" }
#4
39
For a pure Ruby solution combine URI.parse
with CGI.parse
(this can be used even if Rails/Rack etc. are not required):
对于纯Ruby解决方案,请合并URI。解析与CGI。解析(即使不需要Rails/Rack等,也可以使用):
CGI.parse(URI.parse(url).query)
# => {"name1" => ["value1"], "name2" => ["value1", "value2", ...] }
#5
25
There more than one ways, to solve your problem. Others has shown you the some tricks. I know another trick. Here is my try :-
有不止一种方法可以解决你的问题。其他人已经向你展示了一些技巧。我知道另一个技巧。我来试试:-
require 'uri'
url = "http://www.example.com/something?param1=value1¶m2=value2¶m3=value3"
uri = URI(url)
# => #<URI::HTTP:0x89e4898 URL:http://www.example.com/something?param1=value1¶m2=value2¶m3=value3>
URI::decode_www_form(uri.query).to_h # if you are in 2.1 or later version of Ruby
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}
Hash[URI::decode_www_form(uri.query)] # if you are below 2.1 version of Ruby
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}
Read the method docomentation of ::decode_www_form
.
阅读方法说明::decode_www_form。
#6
10
Check out the addressable gem - a popular replacement for Ruby's URI module that makes query parsing easy:
查看可寻址gem—Ruby的URI模块的流行替代品,它使查询解析变得容易:
require "addressable/uri"
uri = Addressable::URI.parse("http://www.example.com/something?param1=value1¶m2=value2¶m3=value3")
uri.query_values['param1']
=> 'value1'
(It also apparently handles param encoding/decoding, unlike URI)
(与URI不同,它显然还处理param编码/解码)
#7
-3
In your Controller, you should be able to access a dictionary (hash) called params
. So, if you know what the names of each query parameter is, then just do params[:param1]
to access it... If you don't know what the names of the parameters are, you could traverse the dictionary and get the keys.
在控制器中,您应该能够访问一个名为params的字典(散列)。因此,如果您知道每个查询参数的名称,那么只需使用params[:param1]来访问它……如果不知道参数的名称,可以遍历字典并获取键。
Some simple examples here.
一些简单的例子。
#1
137
I think you want to turn any given URL string into a HASH?
我想你想把任何给定的URL字符串转换成散列?
You can try http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075
你可以试试http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html M000075
require 'cgi'
CGI::parse('param1=value1¶m2=value2¶m3=value3')
returns
返回
{"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}
#2
137
I found myself needing the same thing for a recent project. Building on Levi's solution, here's a cleaner and faster method:
我发现自己在最近的一个项目中也需要同样的东西。基于Levi's的解决方案,这里有一个更干净、更快的方法:
Rack::Utils.parse_nested_query 'param1=value1¶m2=value2¶m3=value3'
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}
#3
85
Just Improved with Levi answer above -
刚刚改进了以上的Levi回答
Rack::Utils.parse_query URI("http://example.com?par=hello&par2=bye").query
For a string like above url, it will return
对于像上面url这样的字符串,它将返回
{ "par" => "hello", "par2" => "bye" }
#4
39
For a pure Ruby solution combine URI.parse
with CGI.parse
(this can be used even if Rails/Rack etc. are not required):
对于纯Ruby解决方案,请合并URI。解析与CGI。解析(即使不需要Rails/Rack等,也可以使用):
CGI.parse(URI.parse(url).query)
# => {"name1" => ["value1"], "name2" => ["value1", "value2", ...] }
#5
25
There more than one ways, to solve your problem. Others has shown you the some tricks. I know another trick. Here is my try :-
有不止一种方法可以解决你的问题。其他人已经向你展示了一些技巧。我知道另一个技巧。我来试试:-
require 'uri'
url = "http://www.example.com/something?param1=value1¶m2=value2¶m3=value3"
uri = URI(url)
# => #<URI::HTTP:0x89e4898 URL:http://www.example.com/something?param1=value1¶m2=value2¶m3=value3>
URI::decode_www_form(uri.query).to_h # if you are in 2.1 or later version of Ruby
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}
Hash[URI::decode_www_form(uri.query)] # if you are below 2.1 version of Ruby
# => {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3"}
Read the method docomentation of ::decode_www_form
.
阅读方法说明::decode_www_form。
#6
10
Check out the addressable gem - a popular replacement for Ruby's URI module that makes query parsing easy:
查看可寻址gem—Ruby的URI模块的流行替代品,它使查询解析变得容易:
require "addressable/uri"
uri = Addressable::URI.parse("http://www.example.com/something?param1=value1¶m2=value2¶m3=value3")
uri.query_values['param1']
=> 'value1'
(It also apparently handles param encoding/decoding, unlike URI)
(与URI不同,它显然还处理param编码/解码)
#7
-3
In your Controller, you should be able to access a dictionary (hash) called params
. So, if you know what the names of each query parameter is, then just do params[:param1]
to access it... If you don't know what the names of the parameters are, you could traverse the dictionary and get the keys.
在控制器中,您应该能够访问一个名为params的字典(散列)。因此,如果您知道每个查询参数的名称,那么只需使用params[:param1]来访问它……如果不知道参数的名称,可以遍历字典并获取键。
Some simple examples here.
一些简单的例子。