I have a string which contains html markup. As an example:

HTML Code:
<p class="foo">Some text in a paragraph "which may be quoted & have special chars" in it</p>
<p>It is true that <span>1</span> > 5, but not that 1 > 0</p>
As you can see there might be some special characters, and ultimately this HTML is going to be rendered and I don't want plain characters like " and &, i want &quot; and &amp; etc.

Obviously I can't just run this through htmlspecialchars() because that would convert the HTML tags too.

I want to end up with this:

HTML Code:
<p class="foo">Some text in a paragraph &quot;which may be quoted &amp; have special chars&quot; in it</p>
<p>It is true that <span>1</span> &gt; 5, but not that 1 &gt; 0</p>
Does anyone know of a way of converting these characters when they are outside of an HTML tag? I'm thinking it might have to be done using a regex (which isn't my strong point!) since I can't guarantee that the code will be well-formed XML.