I’ve installed a syntax highlighter, but in order for it to work, the tags must be written as < and >. What I need to do is replace all <‘s with < and >’s with > but only inside the PRE tag.
So, in short, I want to escape all HTML characters inside of the pre tag.
Thanks in advance.
tl;dr
You need to parse the input HTML. Use the
DOMDocumentclass to represent your document, parse the input, find all<pre>tags (usingfindElementsByTagName) and escape their content.Code
Unfortunately, the DOM model is very low-level and forces you to iterate the child nodes of the
<pre>tag yourself, to escape them. This looks as follows:Now this function can be used to escape every
<pre>node in the document:Test
Yields:
Note that a DOM always represents a complete document. Hence when the DOM parser gets a document fragment it fills in the missing information. This makes the output potentially different from the input.