我试图解码一些HTML实体,如'<'成为'<'。
我有一个旧的宝石(html_helpers),但它似乎已经被抛弃了两次。
你有什么推荐吗?我需要在模型中使用它。
我试图解码一些HTML实体,如'<'成为'<'。
我有一个旧的宝石(html_helpers),但它似乎已经被抛弃了两次。
你有什么推荐吗?我需要在模型中使用它。
当前回答
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!"
其他回答
你可以使用htmlascii gem:
Htmlascii.convert string
如果你不想添加一个新的依赖项(比如HTMLEntities),并且你已经在使用Hpricot,它可以为你进行转义和反转义。它处理的比CGI多得多:
Hpricot.uxs "foo bär"
=> "foo bär"
我认为Nokogiri宝石也是一个不错的选择。它非常稳定,有一个巨大的贡献社区。
样品:
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!"
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!"
<% str="<h1> Test </h1>" %>
result: < h1 > Test < /h1 >
<%= CGI.unescapeHTML(str).html_safe %>