nothing-special-here nothing-special-here - 2 years ago 91
Ruby Question

Convert HTML to plain text (with inclusion of <br>s)

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

<br />

For example, given this HTML:

<p>ala ma kota</p> <br /> <span>i kot to idiota </span>

I want this output:

ala ma kota
i kot to idiota

When I just call
it excludes
<br />

ala ma kota i kot to idiota

Answer Source

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") }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download