PHP - Removing the effect of HTML tags in string - BUT displaying them as well?

n a picture n a · May 5, 2010 · Viewed 12.3k times · Source

Consider strip_tags() .

strip_tags("<b>TEXT</b>");

Output:

TEXT

But what if i want to nullify the effect of the tags but display them as well?

Output:

<b>TEXT</b>

Would i have to use preg_replace() ? Or is there a more elegant solution available?

Thanks :D

Answer

D&#39;Arcy Rittich picture D'Arcy Rittich · May 5, 2010

You can HTML encode the string via htmlspecialchars:

htmlspecialchars("<b>TEXT</b>");