If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Replacing characters with html codes, but excluding html tags

<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.

Thanks a lot, that seems to work nicely from my initial tests. Is there an easy way of skipping any php content (ie between <?php and ?>) that may exist? This is the only issue I still seem to be having. So for example the following:

Hmmmm yes sometimes the PHP could be fairly complex. I think I might try what you suggest, if I split out the string based on the php <?php and ?> tags, I could then just apply this callback to the non-php portions of the string, then stick it back together after.