Strip specific HTML tags


#1

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

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


#2

Should be able to do that with the strip_html filter.

{{ post.excerpt | strip_html }}

#3

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

<img src="…">
<p>…</p>
<p>…</p>

And I’d like it to look like this:

<p>…</p>
<p>…</p>

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


#4

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.


#5

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


#6

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

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


#7

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


#8

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.