Reputation: 2289
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
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
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
Reputation: 16506
Try using regex like this:
str.gsub!(/<.*?>/, " ")
# => " Class GOesHere SomeExtra Tag "
Upvotes: 4