我从DB中获得了“—的字符。我从中获取的表是latin1字符集。我需要正确地展示这些角色。如何在Ruby on rails上做到这一点?是否有一个函数或一段代码会用正确的代码替换这些字符?
答案 0 :(得分:1)
您可能需要设置数据库字符串的编码。试试encode
的{{1}}方法:
String
如果ISO 8859 1不适合您,还有很多其他编码。如果用户浏览器不支持正确的编码,您可以将选项传递给dbstr.encode("iso-8859-1")
,以使其用encode
等替换未知数。
答案 1 :(得分:0)
我尝试了所有编码,直到找到正确的编码
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