19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

文字列から HTML タグを取り除く

Posted at

文字列から HTML タグを取り除くには正規表現を使ったり Gem (Sanitize など) を利用したりする方法がありますが、
Rails のヘルパにもっと気軽に使えるメソッドがありました。
それは ActionView::Helpers::SanitizeHelper#strip_tags です。

ApplicationController.helpers.strip_tags("<p class='text-muted'>デブだからって<br>力があると<br>思うなよ</p>")
#=> "デブだからって力があると思うなよ"

ビュー以外の任意のクラスで使いたい場合は delegate と併用すると便利かと思います。

delegate :strip_tags, to: 'ApplicationController.helpers'
19
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?