Force strings to UTF-8 from any encoding

前端 未结 4 1362
不知归路
不知归路 2020-12-13 13:36

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 i

4条回答
  •  生来不讨喜
    2020-12-13 13:56

    This will ensure you have the correct encoding and won't error out because it replaces any invalid or undefined character with a blank string.

    This will ensure no matter what, that you have a valid UTF-8 string

    str.encode(Encoding.find('UTF-8'), {invalid: :replace, undef: :replace, replace: ''})
    

提交回复
热议问题