1

我从 DB 中得到像 ’ … – “ †‘ 这样的字符。我从中获取的表是 latin1 字符集。我需要正确显示这些字符。如何在 Ruby on rails 中做到这一点?是否有一个函数或一段代码可以用正确的字符替换这些字符?

4

2 回答 2

1

您可能需要设置数据库字符串的编码。尝试以下encode方法String

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

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

于 2012-06-05T15:32:43.837 回答
1

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

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

于 2018-12-11T12:08:00.457 回答