Search code examples
phphtmlreplacetagsstrip

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


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


Solution

  • You can HTML encode the string via htmlspecialchars:

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