Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove HTML markup from string

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.

like image 568
gabrielhilal Avatar asked Sep 06 '25 16:09

gabrielhilal


2 Answers

You can try this:

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

See strip_tags(html).

like image 132
ant Avatar answered Sep 10 '25 01:09

ant


You can try this:

strip_tags(@string)
like image 29
Manoj Thapliyal Avatar answered Sep 10 '25 01:09

Manoj Thapliyal