【问题标题】:How can I convert character references to UTF-8 strings in Ruby?如何在 Ruby 中将字符引用转换为 UTF-8 字符串?
【发布时间】:2009-06-05 22:04:48
【问题描述】:

我有一些来自提要的内容。在这些提要中,UTF-8 字符通常被编码为字符引用,即“å”是“å”。为了避免在我的视图中对它们进行双重编码(即“å”),我想将它们转换回正常的 UTF_8 字符。如何在 Ruby 中做到这一点?

我想要:

"å".convert_to_utf8 => "å"

【问题讨论】:

  • 也许通过 XML 库发送它,看看它是否会为您解码文本?这些是在 XML 中编码字符的 XML 方式。

标签: ruby utf-8 character


【解决方案1】:

HTMLEntities gem 就是为此而设计的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2012-06-25
    • 2016-09-02
    • 2015-11-14
    • 2010-11-03
    相关资源
    最近更新 更多