<p>You could use Twig's <a href="http://twig.sensiolabs.org/doc/filters/replace.html" rel="nofollow">replace filter</a> on output:</p>

<pre class="lang-twig prettyprint-override"><code>{{ entry.richtext_field_handle|replace('&amp;': '&amp;amp;') }}
</code></pre>

<p>At first glance I couldn't find a <a href="http://imperavi.com/redactor/docs/settings/" rel="nofollow">Redactor setting</a> which would help you, but you might want to check yourself, or even write a <a href="http://imperavi.com/redactor/docs/plugins-creating/" rel="nofollow">Redactor plugin</a>.</p>

CC BY-SA 3.0
2014-08-08T12:59:03.510Z
2014-08-08T12:59:03.510Z
Victor
<p>I finally found a way to do that using the "<a href="https://github.com/victor-in/Craft-TwigPCRE" rel="nofollow noreferrer">TwigPCRE</a>" Craft plugin:</p>

<pre><code>|preg_replace('/&amp;(?!(?:[a-z]+|#x?\d+);)/', '&amp;amp;')|raw
</code></pre>

<p>This will replace all unescaped &amp; characters which are not part of something else.</p>

CC BY-SA 3.0
2014-08-11T07:13:24.843Z
2017-05-24T07:37:48.847Z
2017-05-24T07:37:48.847Z
Andre Mohren
<p><strong>You don't have to escape the ampersand for it to be valid html5</strong>, and if it's appearing in content, not in a URL for example, then you really have nothing to worry about.</p>

<p>This validates:</p>

<pre><code>&lt;!doctype html&gt;
&lt;head&gt;
  &lt;title&gt;E&amp;K&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
 &lt;h1&gt;E&amp;K&lt;/h1&gt;
&lt;/body&gt;
</code></pre>

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

CC BY-SA 3.0
2017-05-24T11:16:34.537Z
2017-05-24T11:16:34.537Z
Seth Warburton
<p>I'm currently finishing a client-website which has to be 100% valid HTML5. For one of the sections, I am using a matrix containing rich-text fields. This content is simply shown on the front page.
The name of the client is "E&amp;K".</p>

<p>Requirements:</p>

<ul>
<li>The output contains HTML tags.</li>
<li>The output has to be 100% valid HTML.</li>
</ul>

<p>Problem:
The &amp; character won't be escaped to &amp;amp; wherever it is outputted. This breaks the valid requirement. It seems the problem is the input, not the output. However the rich text editor keeps replacing manually placed &amp;amp; with &amp;</p>

<p>How can I get this to work correctly?</p>

CC BY-SA 3.0
2017-07-24T15:54:01.767Z
If an answer provided the solution to your question it's good etiquette, and good manners, to mark it as the correct answer.
Seth Warburton
<richtext><escaping>
How to use "&amp;" in richtext?
# How to use "&amp;" in richtext?

(Asked by: username_0 on 2014-08-08)

I'm currently finishing a client-website which has to be 100% valid HTML5. For one of the sections, I am using a matrix containing rich-text fields. This content is simply shown on the front page.
The name of the client is "E&K".

Requirements:

* The output contains HTML tags.
* The output has to be 100% valid HTML.

Problem:
The & character won't be escaped to & wherever it is outputted. This breaks the valid requirement. It seems the problem is the input, not the output. However the rich text editor keeps replacing manually placed & with &

How can I get this to work correctly?

**Tags:** richtext, escaping

> **Comments:**
> **username_1**: If an answer provided the solution to your question it's good etiquette, and good manners, to mark it as the correct answer.  

---

## 3 answers

### Answer by username_2 on 2014-08-08. Score: 2

You could use Twig's replace filter on output:
{{ entry.richtext_field_handle|replace('&': '&') }}

At first glance I couldn't find a Redactor setting which would help you, but you might want to check yourself, or even write a Redactor plugin.


---

### Answer by username_0 on 2014-08-11. Score: 1

I finally found a way to do that using the "TwigPCRE" Craft plugin:
|preg_replace('/&(?!(?:[a-z]+|#x?\d+);)/', '&')|raw

This will replace all unescaped & characters which are not part of something else.


---

### Answer by username_1 on 2017-05-24. Score: 0

You don't have to escape the ampersand for it to be valid html5, and if it's appearing in content, not in a URL for example, then you really have nothing to worry about.
This validates:


  E&K


 E&K


---

