URL编码回真URL

URL encoding back to true URL

我有这些 URL:

http://www.jobbsafari.se/jobbsoekning?q=trainers.php%253Fid%253D'%2Band''
http://www.jobbsafari.se/jobbsoekning?q=trainers.php?id%253D%2Band'
http://tay-sen.com.ua/trainers.php?id=12%26action=show_photos%26m_lang_id=9'

我需要将它们编码回它们的真实 URL。现在我正在使用:

string = ARGV[0]
string.gsub("%25", '%').gsub("%53", "S").gsub(etc..)

有没有更好的方法来执行多个 gsub 而不必执行 .gsub.gsub.gsub

使用 uri:

require 'uri'
URI.decode(your_url)

它将替换所有的URI编码。

问题是您的网址包含经过多次编码的查询。

如果编码发生在你没问题的时候,但是当你有多层时它会变得混乱:

require 'uri'

uri = URI.parse("http://tay-sen.com.ua/trainers.php?id=12%26action=show_photos%26m_lang_id=9")
query = URI.decode_www_form(uri.query).to_h
query  # => {"id"=>"12&action=show_photos&m_lang_id=9"}

URI.decode_www_form(query['id']).to_h
# => {"12"=>"", "action"=>"show_photos", "m_lang_id"=>"9"}