<p>I wrote a plugin called <a href="https://github.com/mmikkel/RetconHTML-Craft" rel="nofollow">Retcon</a> which uses <a href="http://php.net/manual/en/class.domdocument.php" rel="nofollow">DOMDocument</a> to re-write HTML content. There's a couple of ways you could achieve your goal easily using Retcon:  </p>

<p>Remove all header tags (and their content), using the <a href="https://github.com/mmikkel/RetconHTML-Craft/wiki/Remove" rel="nofollow"><code>retconRemove</code></a> filter:</p>

<pre><code>{{ entry.body|retconRemove(['h1','h2','h3','h4','h5','h6']) }}
</code></pre>

<p>Remove everything except paragraphs, using the <a href="https://github.com/mmikkel/RetconHTML-Craft/wiki/Only" rel="nofollow"><code>retconOnly</code></a> filter:</p>

<pre><code>{{ entry.body|retconOnly('p') }}
</code></pre>

<p>Remove everything except paragraphs and images:</p>

<pre><code>{{ entry.body|retconOnly(['p', 'img']) }}
</code></pre>

<p>As for truncating the content, I'd probably add the hacksaw filter after using Retcon, i.e.</p>

<pre><code>{{ entry.body|retconOnly('p')|hacksaw(chars='300',allow='&lt;p&gt;', append='...') }}
</code></pre>

CC BY-SA 3.0
2016-03-01T19:40:43.867Z
2016-03-01T19:46:47.290Z
2016-03-01T19:46:47.290Z
Mats Mikkel Rummelhoff
<p>You could consider replacing all of your heading tags manually with the <a href="https://craftcms.com/docs/templating/filters#replace" rel="nofollow">replace</a> twig filter.</p>

<p>This would replace all headings with a paragraph:
<code>{{ entry.body|replace({'h1': 'p', 'h2': 'p', 'h3': 'p', 'h4': 'p', 'h5': 'p', 'h6': 'p'})|hacksaw(chars='300', allow='&lt;p&gt;', append='...') }}</code></p>

CC BY-SA 3.0
2016-03-01T14:41:31.643Z
Thanks. I actually want to remove the headings rather than have them display as paragraph text. Having the heading text as part of the body doesn't really work
sarah3585
CC BY-SA 3.0
2016-03-01T14:42:26.083Z
You could like to remove the contents of whatever is in the heading tags?
Maarten de Graaf
CC BY-SA 3.0
2016-03-01T14:50:23.403Z
Yes, remove the content with heading tags rather than just the tags.
sarah3585
CC BY-SA 3.0
2016-03-01T14:54:47.113Z
One lazy way of doing that is allowing headings and after that via the power of CSS `display:none` it.
Maarten de Graaf
CC BY-SA 3.0
2016-03-01T15:24:10.297Z
Yes that was my initial thought but agree it's a bit lazy so thought I'd see if there was a better option.
sarah3585
CC BY-SA 3.0
2016-03-01T14:30:45.253Z
2016-03-01T17:52:48.457Z
2016-03-01T17:52:48.457Z
Maarten de Graaf
<p>You could use the Twig <a href="https://craftcms.com/docs/templating/filters#replace" rel="nofollow">replace</a> filter with a regular expression, this'll remove the tag and it's content.</p>

<pre><code>{{ entry.body | replace('/&lt;h([1-6])&gt;.*?&lt;\\/h([1-6])&gt;/', '') | raw | hacksaw(chars='300',allow='&lt;p&gt;', append='...') }} 
</code></pre>

<p>The only limitation is regular expressions aren't fantastic with nested html tags, so your mileage my vary!</p>

<p>You might need to play around with the Regex a bit if you want to just limit it to a few headings, this will strip all h1, h2, h3, h4, h5, h6. You also might not need the <code>| raw</code> filter, it seemed to escape everything after the replace when I quickly tested it.</p>

CC BY-SA 3.0
2016-03-01T16:52:01.307Z
2016-03-01T16:52:01.307Z
Steve Holland
<p>I'm using hacksaw to display the first part of my article on a blog listing page.</p>

<pre><code>{{ entry.body|hacksaw(chars='300',allow='&lt;p&gt;', append='...') }}
</code></pre>

<p>The issue I have is when an article starts with a heading in it's body it looks like the attached. I'd ideally like to remove any headings from the excerpts.</p>

<p><a href="https://i.sstatic.net/d3apI.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/d3apI.png" alt="enter image description here"></a> </p>

<p><a href="https://i.sstatic.net/srSFz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/srSFz.png" alt="enter image description here"></a></p>

<p>As you can see the headings 'Reducing the size...' and 'Scaling' come through as plain text and the rest continues with my allowed <p></p>

<p>The only way I can think to do this is to allow their headings (h2 and h3) and then hide them in CSS, but I'm hoping there's a better way. </p>

<plugin-hacksaw>
Removing headers from blog post "excerpts" (using Hacksaw)
# Removing headers from blog post "excerpts" (using Hacksaw)

(Asked by: username_0 on 2016-03-01)

I'm using hacksaw to display the first part of my article on a blog listing page.
{{ entry.body|hacksaw(chars='300',allow='', append='...') }}

The issue I have is when an article starts with a heading in it's body it looks like the attached. I'd ideally like to remove any headings from the excerpts.
 

As you can see the headings 'Reducing the size...' and 'Scaling' come through as plain text and the rest continues with my allowed 
The only way I can think to do this is to allow their headings (h2 and h3) and then hide them in CSS, but I'm hoping there's a better way.

**Tags:** plugin-hacksaw


---

## 3 answers

### Answer by username_3 on 2016-03-01. Score: 4 | Accepted answer

You could use the Twig replace filter with a regular expression, this'll remove the tag and it's content.
{{ entry.body | replace('/.*?<\\/h([1-6])>/', '') | raw | hacksaw(chars='300',allow='', append='...') }} 

The only limitation is regular expressions aren't fantastic with nested html tags, so your mileage my vary!
You might need to play around with the Regex a bit if you want to just limit it to a few headings, this will strip all h1, h2, h3, h4, h5, h6. You also might not need the | raw filter, it seemed to escape everything after the replace when I quickly tested it.


---

### Answer by username_1 on 2016-03-01. Score: 2

I wrote a plugin called Retcon which uses DOMDocument to re-write HTML content. There's a couple of ways you could achieve your goal easily using Retcon:  
Remove all header tags (and their content), using the retconRemove filter:
{{ entry.body|retconRemove(['h1','h2','h3','h4','h5','h6']) }}

Remove everything except paragraphs, using the retconOnly filter:
{{ entry.body|retconOnly('p') }}

Remove everything except paragraphs and images:
{{ entry.body|retconOnly(['p', 'img']) }}

As for truncating the content, I'd probably add the hacksaw filter after using Retcon, i.e.
{{ entry.body|retconOnly('p')|hacksaw(chars='300',allow='', append='...') }}


---

### Answer by username_2 on 2016-03-01. Score: 2

You could consider replacing all of your heading tags manually with the [replace](https://craftcms.com/docs/templating/filters#replace) twig filter.

This would replace all headings with a paragraph:
`{{ entry.body|replace({'h1': 'p', 'h2': 'p', 'h3': 'p', 'h4': 'p', 'h5': 'p', 'h6': 'p'})|hacksaw(chars='300', allow='

', append='...') }}`

> **Comments:**
> **username_0**: Thanks. I actually want to remove the headings rather than have them display as paragraph text. Having the heading text as part of the body doesn't really work  
> **username_2**: You could like to remove the contents of whatever is in the heading tags?  
> **username_0**: Yes, remove the content with heading tags rather than just the tags.  
> **username_2**: One lazy way of doing that is allowing headings and after that via the power of CSS `display:none` it.  
> **username_0**: Yes that was my initial thought but agree it's a bit lazy so thought I'd see if there was a better option.  

---

