Strip html from string Ruby on Rails

后端 未结 6 1807
孤独总比滥情好
孤独总比滥情好 2020-12-07 16:35

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 ta

6条回答
  •  一整个雨季
    2020-12-07 17:00

    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 Gem

    unsafe_html = "ohai! 
    div is safe
    " doc = Loofah.fragment(unsafe_html).scrub!(:strip) doc.to_s # => "ohai!
    div is safe
    " doc.text # => "ohai! div is safe "

提交回复
热议问题