Avdept
Avdept

Reputation: 2289

Replace html tags with whitespaces

I'm using method strip_tags which removes all tags from my string, however after removing some of my text lacking of whitespaces, especially where 1 tag ends and another begins. Is there any way to insert whitespaces into place where tags were removed? Look for ex. below

str
 => "<span>Class GOesHere</span><div>SomeExtra Tag</div>" 

helper.strip_tags(str)
 => "Class GOesHereSomeExtra Tag" 

Upvotes: 3

Views: 2810

Answers (3)

vee
vee

Reputation: 38645

I would recommend parsing the HTML and extracting the text. Nokogiri, a very well known gem should help solve this with ease:

require 'nokogiri'
=> false
> str = "<span>Class GOesHere</span><div>SomeExtra Tag</div>"
=> "<span>Class GOesHere</span><div>SomeExtra Tag</div>"
> Nokogiri::HTML(str).text
=> "Class GOesHereSomeExtra Tag"

Update:

This will search all text nodes in the html and map the text content. The resultant array is concatenated with a space separator:

> Nokogiri::HTML(str).xpath('//text()').map(&:text).join(' ')
=> "Class GOesHere SomeExtra Tag"

Upvotes: 10

Nermin
Nermin

Reputation: 6100

str
 => "<span>Class GOesHere</span><div>SomeExtra Tag</div>"

You could change your string before striping tags, for example

str.gsub!('><', '> <')

and it should produce

str
 => "<span>Class GOesHere</span> <div>SomeExtra Tag</div>" 

Upvotes: -1

shivam
shivam

Reputation: 16506

Try using regex like this:

str.gsub!(/<.*?>/, " ")
# => " Class GOesHere  SomeExtra Tag "

Upvotes: 4

Related Questions