如何在Ruby中将字符引用转换为UTF-8字符串? [英] How can I convert character references to UTF-8 strings in Ruby?

查看:547
本文介绍了如何在Ruby中将字符引用转换为UTF-8字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些来自Feed的内容。在这些Feed中,UTF-8字符通常被编码为字符引用,即å是å。为了避免在我的视图(即& amp;#xE5;)中进行双重编码,我想将它们转换回正常的UTF_8字符​​。我如何在Ruby中这样做?

I have some content from feeds. In these feeds, UTF-8 characters are often encoded as character references, ie "å" is "å". To avoid double encoding these in my views (ie "å") I want to convert these back to normal UTF_8 characters. How can I do this in Ruby?

我想要:

å。convert_to_utf8 =>å

"å".convert_to_utf8 => "å"

推荐答案

HTMLEntities gem旨在做到这一点。

The HTMLEntities gem is designed to do just this.

require 'htmlentities'
coder = HTMLEntities.new
string = "élan"
coder.decode(string) # => "élan"
# or
string.decode_entities # => "élan"

这篇关于如何在Ruby中将字符引用转换为UTF-8字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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