如何在 Ruby on Rails 中正确渲染特殊字符



我从DB得到像"€™"这样的字符。我从中获取的表是拉丁 1 字符集。我需要正确显示这些字符。如何在 Ruby on rails 中做到这一点?是否有一个函数或代码段可以用正确的字符替换这些字符?

您可能需要设置 DB 字符串的编码。尝试encode String方法:

dbstr.encode("iso-8859-1")

如果ISO 8859 1不适合您,还有很多其他编码。如果用户浏览器不支持正确的编码,则可以将一些选项传递给encode,使其用? s等替换未知数。

我已经尝试了所有编码,我找到了正确的编码

text.encode('windows-1250').force_encoding("UTF-8")
text.encode('utf-7').force_encoding("UTF-8")
text.encode('ibm852').force_encoding("UTF-8")
text.encode('shift_jis').force_encoding("UTF-8")
text.encode('iso-2022-jp').force_encoding("UTF-8")
text.encode("Windows-1252").force_encoding("UTF-8")
text.encode("latin1").force_encoding("UTF-8")
text.encode("ISO-8859-1").force_encoding("UTF-8")
text.encode("ISO-8859-2").force_encoding("UTF-8")
text.encode("ISO-8859-3").force_encoding("UTF-8")
text.encode("ISO-8859-4").force_encoding("UTF-8")
text.encode("ISO-8859-5").force_encoding("UTF-8")
text.encode("ISO-8859-6").force_encoding("UTF-8")
text.encode("ISO-8859-7").force_encoding("UTF-8")
text.encode("ISO-8859-8").force_encoding("UTF-8")
text.encode("ISO-8859-9").force_encoding("UTF-8")
text.encode("ISO-8859-10").force_encoding("UTF-8")
text.encode("ISO-8859-11").force_encoding("UTF-8")
text.encode("ISO-8859-12").force_encoding("UTF-8")
text.encode("ISO-8859-13").force_encoding("UTF-8")
text.encode("ISO-8859-14").force_encoding("UTF-8")
text.encode("ISO-8859-15").force_encoding("UTF-8")

然后我创建了无效字符的地图并使用脚本替换了它们(灵感来自 https://markmcb.com/2011/11/07/replacing-with-utf-8-characters-in-ruby-on-rails/)

def fix(text)
  replacements = [
    ['–',           "—"],
    ["—",           "–"],
    ["‘",           "‘"],
    ['…',           '…'],
    ['’',           '’'],
    ['“',           '“'],
    [/â€[[:cntrl:]]/, '”'],
    ['â€?',           '”'],
    ['”',           '”'],
    ['“',           '“'],
    ['
',           '—'], # not sure about this one
    ['″',           '″'],
    ['‎',           ''], # emtpy str
    [' ',           ''], # emtpy str
    [' ',           ''], # emtpy str
    ['​',           ''], # emtpy str
    ['â€',           ''], # emtpy str
    ["â€s'",           ''], # emtpy str
  ]
  new_text = text
  replacements.each { |set| new_text = new_text.gsub(set[0], set[1]) }
  new_text
end
# rails automatically will check if publication was changed and won't save if it wasn't changed
Publication.where('content like ?', "%â€%").find_each do |publication|
  publication.title         = fix(publication.title)
  publication.content       = fix(publication.content)
  publication.short_content = fix(publication.short_content)
  publication.save!
end

直到 Publication.where('content like ?', "%â€%").count 等于 0

最新更新