Convert HTML to plain text (with inclusion of
s)

后端 未结 5 670
-上瘾入骨i
-上瘾入骨i 2021-02-06 01:39

Is it possible to convert HTML with Nokogiri to plain text? I also want to include
tag.

For example, given this HTML:

5条回答
  •  被撕碎了的回忆
    2021-02-06 02:12

    Instead of writing complex regexp I used Nokogiri.

    Working solution (K.I.S.S!):

    def strip_html(str)
      document = Nokogiri::HTML.parse(str)
      document.css("br").each { |node| node.replace("\n") }
      document.text
    end
    

提交回复
热议问题