26

Let's say I have:

@string = "it is a <a href="#">string</a>"

I want to use it in different parts of my application in two ways:

  • With a clickable link
  • Without the clickable link (but not showing any HTML markup)

The first one can be done using html_safe:

@string.html_safe

It is a string

How can I achieve the second one?

It is a string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • Possible Dupilcate: http://stackoverflow.com/questions/7414267/strip-html-from-string-ruby-on-rails – abhijit Mar 06 '13 at 16:05

7 Answers7

54

You can try this:

ActionView::Base.full_sanitizer.sanitize(@string)

See strip_tags(html).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ant
  • 22,634
  • 36
  • 132
  • 182
10

You can try this:

strip_tags(@string)
Manoj Thapliyal
  • 567
  • 7
  • 9
7

For general-purpose use (e.g. web scraper):

puts Rails::Html::FullSanitizer.new.sanitize("<div>Hello</div><br>")
# Hello
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Benny
  • 11,466
  • 1
  • 18
  • 6
2

You can use nokogiri to do the same.

This SO post tells the story.

Here in short:

This uses the XPath's starts-with function:

You have to first define it like this:

require 'nokogiri'

item = Nokogiri::HTML('<a href="#">string</a>')
puts item.to_html

The above will give the html output. Then you can use XPath.

item.search('//a[not(starts-with(@href, "http://"))]').each do |a|
  a.replace(a.content)
end
puts item.to_html
Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
1

In Rails, see also the strip_tags method. http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags

piratebroadcast
  • 291
  • 5
  • 15
0

Rails provides a method called strip_links, which seems to do what you want (looking at its name).

According to its APIDock page it is a bit limited. To make it applicable to a/any string you could extend the string class:

class String
  def strip_links
    ActionController::Base.helpers.strip_links(self)
  end
end

So you can use:

@string.strip_links
Veger
  • 37,240
  • 11
  • 105
  • 116
  • strip_links gives an error if the string has no html markups. Extending the method don't give the error, but does not work for some markups, such as . But thanks anyway. – gabrielhilal Mar 06 '13 at 16:08
  • Oh... I thought/assumed you always have a link in your string... I guess the [`sanitize` method](http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-sanitize) removes all HTML... (It is in the same Helper module) – Veger Mar 06 '13 at 16:10
0

Inspired by upstairs, I define this function in my project

 def delete_html_markup(data)
    return data if data.blank?
    if data.is_a?(Array)
      data.map{ | s |  delete_html_markup(s)  }
    elsif data.is_a?(Hash)
      data.each do | k, v |
        data[k] = delete_html_markup(v)
      end
    else
      ActionView::Base.full_sanitizer.sanitize(data)
    end
  end
lim
  • 13
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30609974) – mbuechmann Dec 21 '21 at 14:14