4

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

我想:

"å".convert_to_utf8 => "å"

4

1 回答 1

6

HTMLEntities gem 就是为此而设计的。

require 'htmlentities'
coder = HTMLEntities.new
string = "élan"
coder.decode(string) # => "élan"
# or
string.decode_entities # => "élan"
于 2009-06-05T22:20:18.147 回答