从字符串 Ruby on Rails 中去除 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7414267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strip html from string Ruby on Rails
提问by Mattias
I'm working with Ruby on Rails, Is there a way to strip html
from a string using sanitize or equal method and keep only text inside value attribute on input tag?
我正在使用 Ruby on Rails,有没有办法html
使用 sanitize 或 equal 方法从字符串中剥离并仅保留输入标签上的 value 属性中的文本?
回答by Jon
If we want to use this in model
如果我们想在模型中使用它
ActionView::Base.full_sanitizer.sanitize(html_string)
which is the code in "strip_tags" method
这是“strip_tags”方法中的代码
回答by Michael Kohl
There's a strip_tags
method in ActionView::Helpers::SanitizeHelper
:
有一个strip_tags
方法ActionView::Helpers::SanitizeHelper
:
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags
Edit: for getting the text inside the value attribute, you could use something like Nokogiri with an Xpath expression to get that out of the string.
编辑:为了获取 value 属性中的文本,您可以使用类似 Nokogiri 和 Xpath 表达式的东西将其从字符串中取出。
回答by bcackerman
Yes, call this: sanitize(html_string, tags:[])
是的,称之为: sanitize(html_string, tags:[])
回答by Satishakumar Awati
ActionView::Base.full_sanitizer.sanitize(html_string)
White list of tags and attributes can be specified as bellow
标签和属性的白名单可以指定如下
ActionView::Base.full_sanitizer.sanitize(html_string, :tags => %w(img br p), :attributes => %w(src style))
Above statement allows tags img, brand pand attributes srcand style.
以上语句允许标签img、br和p以及属性src和style。
回答by Krishna Vedula
I've used the Loofah library, as it is suitable for both HTML and XML (both documents and string fragments). It is the engine behind the html sanitizer gem. I'm simply pasting the code example to show how simple it is to use.
我使用了 Loofah 库,因为它适用于 HTML 和 XML(文档和字符串片段)。它是 html sanitizer gem 背后的引擎。我只是粘贴代码示例来展示它的使用有多简单。
unsafe_html = "ohai! <div>div is safe</div> <script>but script is not</script>"
doc = Loofah.fragment(unsafe_html).scrub!(:strip)
doc.to_s # => "ohai! <div>div is safe</div> "
doc.text # => "ohai! div is safe "
回答by josetapadas
How about this?
这个怎么样?
white_list_sanitizer = Rails::Html::WhiteListSanitizer.new
WHITELIST = ['p','b','h1','h2','h3','h4','h5','h6','li','ul','ol','small','i','u']
[Your, Models, Here].each do |klass|
klass.all.each do |ob|
klass.attribute_names.each do |attrs|
if ob.send(attrs).is_a? String
ob.send("#{attrs}=", white_list_sanitizer.sanitize(ob.send(attrs), tags: WHITELIST, attributes: %w(id style)).gsub(/<p>\s*<\/p>\r\n/im, ''))
ob.save
end
end
end
end