如何在 Ruby 中编码/解码 HTML 实体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1600526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I encode/decode HTML entities in Ruby?
提问by Kostas
I am trying to decode some HTML entities, such as '<'
becoming '<'
.
我正在尝试解码一些 HTML 实体,例如'&lt;'
成为'<'
.
I have an old gem (html_helpers) but it seems to have been abandoned twice.
我有一个旧 gem ( html_helpers),但它似乎已被放弃两次。
Any recommendations? I will need to use it in a model.
有什么建议吗?我需要在模型中使用它。
采纳答案by Ivailo Bardarov
HTMLEntitiescan do it:
HTMLEntities可以做到:
: jmglov@laurana; sudo gem install htmlentities
Successfully installed htmlentities-4.2.4
: jmglov@laurana; irb
irb(main):001:0> require 'htmlentities'
=> []
irb(main):002:0> HTMLEntities.new.decode "¡I'm highly annoyed with character references!"
=> "?I'm highly?annoyed with character references!"
回答by Damien MATHIEU
To encode the characters, you can use CGI.escapeHTML
:
要对字符进行编码,您可以使用CGI.escapeHTML
:
string = CGI.escapeHTML('test "escaping" <characters>')
To decode them, there is CGI.unescapeHTML
:
要解码它们,有CGI.unescapeHTML
:
CGI.unescapeHTML("test "unescaping" <characters>")
Of course, before that you need to include the CGI library:
当然,在此之前您需要包含 CGI 库:
require 'cgi'
And if you're in Rails, you don't need to use CGI to encode the string. There's the h
method.
如果您使用 Rails,则不需要使用 CGI 对字符串进行编码。还有的h
方法。
<%= h 'escaping <html>' %>
回答by Hoang Le
I think Nokogiri gemis also a good choice. It is very stable and has a huge contributing community.
我认为Nokogiri gem也是一个不错的选择。它非常稳定,并拥有庞大的贡献社区。
Samples:
样品:
a = Nokogiri::HTML.parse "foo bär"
a.text
=> "foo?b?r"
or
或者
a = Nokogiri::HTML.parse "¡I'm highly annoyed with character references!"
a.text
=> "?I'm highly?annoyed with character references!"
回答by memonk
To decode characters in Rails use:
要在 Rails 中解码字符,请使用:
<%= raw '<html>' %>
So,
所以,
<%= raw '<br>' %>
would output
会输出
<br>
回答by Jason L Perry
If you don't want to add a new dependency just to do this (like HTMLEntities
) and you're already using Hpricot
, it can both escape and unescape for you. It handles much more than CGI
:
如果您不想添加新的依赖来执行此操作(例如HTMLEntities
)并且您已经在使用Hpricot
,它可以为您转义和取消转义。它处理的不仅仅是CGI
:
Hpricot.uxs "foo bär"
=> "foo?b?r"
回答by kartouch
You can use htmlascii
gem:
您可以使用htmlascii
宝石:
Htmlascii.convert string
回答by Usman
<% str="<h1> Test </h1>" %>
result: < h1 > Test < /h1 >
<%= CGI.unescapeHTML(str).html_safe %>