class method
URI.encode_www_form_component
(::_ToS str, ?::encoding? enc) -> ::String
encode_www_form_component(str, enc=nil)
lib/uri/common.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/uri/common.rb, line 397
def self.encode_www_form_component(str, enc=nil)
_encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP_, str, enc)
end
Returns a URL-encoded string derived from the given string str.
The returned string:
-
Preserves:
-
Characters
'*','.','-', and'_'. -
Character in ranges
'a'..'z','A'..'Z', and'0'..'9'.
Example:
ExampleURI.encode_www_form_component('*.-_azAZ09') # => "*.-_azAZ09"
-
-
Converts:
-
Character
' 'to character'+'. -
Any other character to “percent notation”; the percent notation for character c is
'%%%X' % c.ord.
Example:
ExampleURI.encode_www_form_component('Here are some punctuation characters: ,;?:') # => "Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A"
-
Encoding:
-
If
strhas encoding Encoding::ASCII_8BIT, argumentencis ignored. -
Otherwise
stris converted first to Encoding::UTF_8 (with suitable character replacements), and then to encodingenc.
In either case, the returned string has forced encoding Encoding::US_ASCII.
Related: URI.encode_uri_component (encodes ' ' as '%20').
See also
.encode_www_form
.for
.encode_uri_component
.join
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0