How can I convert character references to UTF-8 strings in Ruby?

断了今生、忘了曾经 提交于 2019-12-23 13:09:17

问题


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?

I want:

"å".convert_to_utf8 => "å"


回答1:


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"


来源:https://stackoverflow.com/questions/958361/how-can-i-convert-character-references-to-utf-8-strings-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!