Convert angle brackets to HTML entities without modifying tags in PHP

72 views Asked by At

There are plenty of similar question here on StackOverlow, but could not find a solution to my particular problem.

I'm facing a challenge with encoding using PHP angle brackets (<,>) to HTML entities in a string that contains HTML content. The goal is to encode only the angle brackets not 'belonging' to any tag, while leaving the HTML tags untouched. Here's an example:

<p> sample text with equation like b<a or text <inside> brackets </p>

Regex solutions won't work because of too many variations. Tried HTMLPurifier, but it strips anything resembling tags. I think I need a smart enough DOM parser and then encode all text nodes. Any suggestions?

0

There are 0 answers