0

HTML::FullSanitizer我在 Rails控制台上使用 Rails 遇到了这个错误:

h = HTML::FullSanitizer.new
html = "Something with invalid characters \x80 and tags ī."
h.sanitze html

ArgumentError: invalid byte sequence in UTF-8
from /Users/benaluan/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_controller/vendor/html-scanner/html/sanitizer.rb:37:in `sanitize'

我尝试的是在清理之前对 html 进行编码:

html = html.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')

它有效,但是,它删除了ī字符。有没有人遇到过同样的问题?

4

1 回答 1

1

阅读这篇准确描述您的问题的文章:http ://www.spacevatican.org/2012/7/7/stripping-invalid-utf-8/

本文的解决方案代码:

html = html.force_encoding('UTF-8').
      encode('UTF-16', :invalid => :replace, :replace => '').
      encode('UTF-8')
于 2014-01-27T02:34:51.167 回答