Strip specific HTML tags


Can I strip specific HTML tags from a piece of content?

I’d like to display post.excerpt but without any images (img tags).


Should be able to do that with the strip_html filter.

{{ post.excerpt | strip_html }}


That won’t work in my case. I have something like this:

<img src="…">

And I’d like it to look like this:


If I strip the HTML it will combine the two paragraphs.


You’ll likely need a plugin to do this then. There’s the remove filter but that’s not going to get you very far.


Indeed. That won’t do at all :slight_smile:


You can use
{{ post.excerpt | remove: '<img>' | remove: '<br>' }}
for example.

You can take a closer look at this page for docs:


Oh you don’t really need to strip line breaks, sorry. Here’s your code:
{{ post.excerpt | remove: '<img>' }}


I haven’t tried this out :blush:, but my understanding is that the code you suggest will literally remove any text that reads <img>. I don’t have any such text. I have actual image tags, which include src="…" and so on.