1

I have a url, "localhost/test/http://myimage.com/" (I'm passing myimage.com, because it's hosted on another site and I'm accessing it via an api) my question is how do I go about encoding the image portion of the URL? I thought about doing a gsub on the '.' and '/' and then gsubing them back, but I'm wondering if there's an easier way. Thanks for your help.

4

2 回答 2

4

你可以使用URI::encode_www_form_component(str)URI::decode_www_form_component

检查:http ://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html

于 2012-08-15T02:15:17.377 回答
1

您可以使用 uri 库来转义和取消转义 url

require 'uri'
escaped = URI.escape(data, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

你可以用

original = URI.unescape(escaped)
于 2012-08-15T02:33:01.120 回答