Ruby中的url_encode [英] url_encode in Ruby
问题描述
我阅读了 url_encode
的文档./p>
是否有一个表格,可以准确地告诉我使用url_encode
将哪个字符编码成什么字符?
ERB的 收件人: 此外,Ruby的CGI和URI模块具有对URL进行编码,将受限字符转换为实体的功能,因此请不要忽视它们的功能. 例如,转义URL参数的字符: Ruby CGI的 您还可以覆盖它并更改正则表达式,或者在重新定义方法时将其公开以供您自己使用: 而且,类似于覆盖 I read the documentation of Is there a table that tells me exactly which character is encoded to what, using ERB's to: In addition, Ruby's CGI and URI modules have the ability to encode URLs, converting restricted characters to entities, so don't overlook their offerings. For instance, escaping characters for URL parameters: Ruby CGI's You also override that and change the regex, or expose it for your own use inside your redefinition of the method: And, similarly to overriding
这篇关于Ruby中的url_encode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!def url_encode(s, regex=%r[^a-zA-Z0-9_\-.]/)
s.to_s.dup.force_encoding("ASCII-8BIT").gsub(regex) {
sprintf("%%%02X", $&.unpack("C")[0])
}
end
url_encode('pop', /./)
=> "%70%6F%70"
CGI.escape('http://www.example.com')
=> "http%3A%2F%2Fwww.example.com"
CGI.escape('<body><p>foo</p></body>')
=> "%3Cbody%3E%3Cp%3Efoo%3C%2Fp%3E%3C%2Fbody%3E"
escape
还使用一个小的正则表达式来确定应在URL中转义哪些字符.这是文档中方法的定义:def CGI::escape(string)
string.gsub(%r([^ a-zA-Z0-9_.-]+)/) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end.tr(' ', '+')
end
def CGI::escape(string, escape_regex=%r([^ a-zA-Z0-9_.-]+)/)
string.gsub(escape_regex) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end.tr(' ', '+')
end
URI.encode_www_form_component
也有类似的编码,唯一的区别是*
和:
URI.encode_www_form_component('<p>foo</p>')
=> "%3Cp%3Efoo%3C%2Fp%3E"
CGI::escape
,您可以覆盖URI.encode_www_form_component
中的正则表达式:def self.encode_www_form_component(str, regex=%r[^*\-.0-9A-Z_a-z]/)
str = str.to_s
if HTML5ASCIIINCOMPAT.include?(str.encoding)
str = str.encode(Encoding::UTF_8)
else
str = str.dup
end
str.force_encoding(Encoding::ASCII_8BIT)
str.gsub!(regex, TBLENCWWWCOMP_)
str.force_encoding(Encoding::US_ASCII)
end
url_encode
.url_encode
?url_encode
can be tweaked:def url_encode(s)
s.to_s.dup.force_encoding("ASCII-8BIT").gsub(%r[^a-zA-Z0-9_\-.]/) {
sprintf("%%%02X", $&.unpack("C")[0])
}
end
def url_encode(s, regex=%r[^a-zA-Z0-9_\-.]/)
s.to_s.dup.force_encoding("ASCII-8BIT").gsub(regex) {
sprintf("%%%02X", $&.unpack("C")[0])
}
end
url_encode('pop', /./)
=> "%70%6F%70"
CGI.escape('http://www.example.com')
=> "http%3A%2F%2Fwww.example.com"
CGI.escape('<body><p>foo</p></body>')
=> "%3Cbody%3E%3Cp%3Efoo%3C%2Fp%3E%3C%2Fbody%3E"
escape
also uses a small regex to figure out which characters should be escaped in a URL. This is the method's definition from the documentation:def CGI::escape(string)
string.gsub(%r([^ a-zA-Z0-9_.-]+)/) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end.tr(' ', '+')
end
def CGI::escape(string, escape_regex=%r([^ a-zA-Z0-9_.-]+)/)
string.gsub(escape_regex) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end.tr(' ', '+')
end
URI.encode_www_form_component
also does a similar encoding, the only differences in characters are *
and :
URI.encode_www_form_component('<p>foo</p>')
=> "%3Cp%3Efoo%3C%2Fp%3E"
CGI::escape
, you can override the regex in URI.encode_www_form_component
:def self.encode_www_form_component(str, regex=%r[^*\-.0-9A-Z_a-z]/)
str = str.to_s
if HTML5ASCIIINCOMPAT.include?(str.encoding)
str = str.encode(Encoding::UTF_8)
else
str = str.dup
end
str.force_encoding(Encoding::ASCII_8BIT)
str.gsub!(regex, TBLENCWWWCOMP_)
str.force_encoding(Encoding::US_ASCII)
end