<p>I don't think you can - by hand is the only way I've found!</p>

CC BY-SA 3.0
2017-01-18T15:41:30.003Z
Thx, your are right. I posted my "by hand" solution as answer.
Mario
CC BY-SA 3.0
2017-01-18T10:56:09.527Z
2017-01-18T10:56:09.527Z
darylknight
<p>I ended up using this solution:</p>

<h3>Filter empty tags in Craft/Twig template</h3>

<pre><code>{{ entry.houseDescription|replace('/&lt;[^\\/&gt;][^&gt;]*&gt;&lt;\\/[^&gt;]+&gt;/', '')|raw }}
</code></pre>

<ol>
<li>The <a href="https://craftcms.com/docs/templating/filters#replace" rel="nofollow noreferrer"><code>replace</code></a> filter <code>/&lt;[^\\/&gt;][^&gt;]*&gt;&lt;\\/[^&gt;]+&gt;/</code> searches for empty tags and removes them.</li>
<li>The <a href="http://twig.sensiolabs.org/doc/2.x/filters/raw.html" rel="nofollow noreferrer"><code>raw</code></a> filter stops Twig from escaping HTML tags.</li>
</ol>

<p><a href="http://regexr.com/3f3dp" rel="nofollow noreferrer">Regular expression test (RegExr)</a></p>

CC BY-SA 3.0
2017-01-18T13:12:35.117Z
2017-01-18T13:20:24.377Z
2017-01-18T13:20:24.377Z
Mario
<p>What is the preferred method to remove an empty tag from the content of a rich text element?</p>

<p>Example result:</p>

<pre><code>&lt;p&gt;
  Lorem&lt;a href="…"&gt;&lt;/a&gt; ipsum
&lt;/p&gt;
</code></pre>

<p>When editing text in the WYSIWYG editor (Redactor) the result can contain empty anchor tags. This is a problem when navigating the page with the tab key, for instance, because the user can not see the focus anymore.</p>

<p>How can I automatically remove those artefacts?</p>

CC BY-SA 3.0
2017-01-13T15:56:35.893Z
one way is to allow HTML in the redactor config and check it over for these things, but that's not great or automatic. You could do something like *:empty{display:none} in the css but that's not great either (as a workmate of mine said, that's just sweeping the turd under the rug)
billythekid
<templating><redactor>
How to remove empty tags in rich text? (Redactor or Twig)
# How to remove empty tags in rich text? (Redactor or Twig)

(Asked by: username_0 on 2017-01-13)

What is the preferred method to remove an empty tag from the content of a rich text element?
Example result:

  Lorem ipsum


When editing text in the WYSIWYG editor (Redactor) the result can contain empty anchor tags. This is a problem when navigating the page with the tab key, for instance, because the user can not see the focus anymore.
How can I automatically remove those artefacts?

**Tags:** templating, redactor
> **Comments:**
> **username_1**: one way is to allow HTML in the redactor config and check it over for these things, but that's not great or automatic. You could do something like *:empty{display:none} in the css but that's not great either (as a workmate of mine said, that's just sweeping the turd under the rug)  

---

## 2 answers

### Answer by username_0 on 2017-01-18. Score: 4 | Accepted answer

I ended up using this solution:
Filter empty tags in Craft/Twig template
{{ entry.houseDescription|replace('/<[^\\/>][^>]*><\\/[^>]+>/', '')|raw }}


The replace filter /<[^\\/>][^>]*><\\/[^>]+>/ searches for empty tags and removes them.
The raw filter stops Twig from escaping HTML tags.

Regular expression test (RegExr)


---

### Answer by username_2 on 2017-01-18. Score: 0

I don't think you can - by hand is the only way I've found!

> **Comments:**
> **username_0**: Thx, your are right. I posted my "by hand" solution as answer.  

---

