如何在常规 Ruby 代码(非 Rails)中使用 strip_tags?

2022-01-18 00:00:00 tags ruby html strip actionview

我需要将 HTML 转换为纯文本.ActionView 的 SanitizeHelper 中有一个很好的功能,但我无法理解如何引用它并在简单的 test.rb 文件中使用它.

I need to turn HTML into plain text. There's a nice function that does that in ActionView's SanitizeHelper, but I have trouble understanding how I can reference it and use it in a simple test.rb file.

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

我希望能够调用 strip_tags("<b>lol</b>") =>哈哈"

推荐答案

这个问题很老了,但我最近也遇到了同样的问题.我找到了一个简单的解决方案:gem sanitize.它很轻,工作正常,并且如果您需要它们还有其他选项.

The question is quite old, but I had the same problem recently. I found a simple solution: gem sanitize. It's light, works fine and has additional options if you need them.

Sanitize.clean("<b>lol</b>") #=> "lol"

相关文章