instance method
CGI::Escape#unescape
(::string str, ?::encoding encoding) -> ::String
unescape(string, encoding = @@accept_charset)
lib/cgi/escape.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/cgi/escape.rb, line 41
def unescape(string, encoding = @@accept_charset)
str = string.tr('+', ' ')
str = str.b
str.gsub!(/((?:%[0-9a-fA-F]{2})+)/) do |m|
[m.delete('%')].pack('H*')
end
str.force_encoding(encoding)
str.valid_encoding? ? str : str.force_encoding(string.encoding)
end
URL-decode an application/x-www-form-urlencoded string with encoding(optional).
Example
string = CGI.unescape("%27Stop%21%27+said+Fred") # => "'Stop!' said Fred"
See also
#h
#unescape_element
#escapeURIComponent
#unescapeElement
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0