开发者

how to change a string to http form in ruby? such as # to %23

How to change a string to http form in ruby?

Such as # to %2开发者_运维技巧3.


Use the CGI class to do this

 url_encoded_string = CGI::escape("'Stop!' said Fred")
     # => "%27Stop%21%27+said+Fred"

Reference http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000067


Use the Ruby Standard CGI library escape method:

require 'cgi'
CGI::escape("#") // => "%23"


URI::escape seems to do it.


I would suggest

URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

its safe to use if you also have URLs in your query params :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜