本文介绍了将字符串从任何编码强制转换为 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 rails 应用程序中,我正在处理来自世界各地的 RSS 提要,并且一些提要具有非 UTF-8 格式的链接.原始提要链接不受我控制,为了在应用的其他部分使用它们,它们需要采用 UTF-8.

In my rails app I'm working with RSS feeds from all around the world, and some feeds have links that are not in UTF-8. The original feed links are out of my control, and in order to use them in other parts of the app, they need to be in UTF-8.

如何检测编码并转换为 UTF-8?

How can I detect encoding and convert to UTF-8?

推荐答案

Ruby 1.9

强制"编码很容易,但它不会转换字符,只是更改编码:

"Forcing" an encoding is easy, however it won't convert the characters just change the encoding:

str = str.force_encoding('UTF-8')

str.encoding.name # => 'UTF-8'

如果要执行转换,请使用encode:

If you want to perform a conversion, use encode:

begin
  str.encode("UTF-8")
rescue Encoding::UndefinedConversionError
  # ...
end

我肯定会阅读以下帖子以获取更多信息:
http://graysoftinc.com/character-encodings/ruby-19s-string

I would definitely read the following post for more information:
http://graysoftinc.com/character-encodings/ruby-19s-string

这篇关于将字符串从任何编码强制转换为 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 23:05