开发者

How to get url parameters from a url string?

开发者 https://www.devze.com 2023-01-24 06:37 出处:网络
Say I have a text \"favorite video url\" field in my app, I need a w开发者_运维百科ay to tell rails that this is a url and then get be able to get parameters from it,

Say I have a text "favorite video url" field in my app, I need a w开发者_运维百科ay to tell rails that this is a url and then get be able to get parameters from it,

for instance, if the user inputs http://www.youtube.com/watch?v=RV0-DgsDLhs

I want to be able to get the 'v'param and store it in a variable.

Please help


require 'uri'
require 'cgi'

uri = 'http://www.youtube.com/watch?v=RV0-DgsDLhs'

params = CGI::parse( URI::parse(uri).query )

puts params.inspect # => {"v"=>["RV0-DgsDLhs"]}
puts params['v'][0] # => RV0-DgsDLhs

Why some of this functionality is in URI and some of it is in CGI is beyond me, but there you have it.

0

精彩评论

暂无评论...
验证码 换一张
取 消