I’ve been re-writing my website lately and added a Syntax highlighter so that I can post code snippets. Before, all I did was htmlentities() the string so that it would be safe and not break anything, but now that I have to use a <pre> to highlight code, htmlentites() effectively removes the syntax highlighting from the page. I’ve been trying to come up with a function that will just perform an htmlentites() on anything between two tags (<entitiesparse> </entitiesparse>) but nothing seems to work. Does anyone know of a function that I can either:
a) Set it to htmlentities() everything but specific tags (like strip_tags())
OR
b) Only htmlentities() things in certain tags (As mentioned above)
You only need to apply
htmlentities()to the raw content. So you can applyhtmlentities()to the raw content (the article text) and then invoke a function to add syntax highlighting after that. So long as you check that your syntax highlighting code cannot introduce unexpected nasties, you don’t need to callhtmlentities()again.And if you’re saying that you use the
aelement to highlight code, I strongly suggest you use thecodeelement instead, which is designed to provide markup for lines or blocks of programming code. Theaelement should only be used as an anchor for a hyperlink.For instance, you could use
Then you could use a cascading style sheet to provide background colour for any element of type
codewith class equal to “highlighted-code”, for instance: