Convert HTML to correct text?

Is there a way to convert HTML to the correct text? I tried everything: from raw to sanitation and even with a mail stone using the text_part method, which should do just that, but it does not work for me.

My best shot so far has been strip_tags(strip_links(resource.body)) , but <p> , <ul> etc. were not converted correctly.

This is more or less what I have in HTML:

 Hello This is some text. Blah blah blah. Address: John Doe 10 ABC Street Whatever City New Features - Feature A - Feature B - Feature C Check this out: http://www.google.com Best, Admin 

which translates to something like

 Hello This is some text. Blah blah blah. Address: John Doe 10 ABC Street Whatever City New Features Feature A Feature B Feature C Check this out: http://www.google.com Best, Admin 

Any idea?

+7
ruby-on-rails ruby-on-rails-3 html-to-text
source share
2 answers
+5
source share

Rails 4.2.1 has #strip_tags , a built-in method specifically designed to remove HTML tags.

Some examples:

 strip_tags("Strip <i>these</i> tags!") 

=> Separate these tags!

 strip_tags("<b>Bold</b> no more! <a href='more.html'>See more here</a>...") 

=> Bold no more! More details

 strip_tags("<div id='top-bar'>Welcome to my website!</div>") 

=> Welcome to my site!

Check this out in the API docs .

+10
source share

All Articles