如何在Ruby中编码/解码HTML实体? [英] How do I encode/decode HTML entities in Ruby?

查看:81
本文介绍了如何在Ruby中编码/解码HTML实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解码一些HTML实体,例如'& lt;'变成'<'

我有一个老宝石( html_helpers ),但它似乎已被放弃两次。



任何建议?我将需要在模型中使用它。

解决方案

HTMLEntities 可以做到这一点:

 :jmglov @ laurana; sudo gem install htmlentities 
成功安装htmlentities-4.2.4
:jmglov @ laurana; irb
irb(main):001:0>要求'htmlentities'
=> []
irb(main):002:0> HTMLEntities.new.decode& iexcl;我很高兴并且带有人物参考!
=> 我非常讨厌角色参考!


I am trying to decode some HTML entities, such as '&amp;lt;' becoming '<'.

I have an old gem (html_helpers) but it seems to have been abandoned twice.

Any recommendations? I will need to use it in a model.

解决方案

HTMLEntities can do it:

: 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 "&iexcl;I&#39;m highly&nbsp;annoyed with character references!"
=> "¡I'm highly annoyed with character references!"

这篇关于如何在Ruby中编码/解码HTML实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆