Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 432
n a Avatar asked May 05 '10 19:05

n a


2 Answers

You can HTML encode the string via htmlspecialchars:

htmlspecialchars("<b>TEXT</b>");
like image 106
D'Arcy Rittich Avatar answered Oct 19 '22 22:10

D'Arcy Rittich


You can easily convert characters to their HTML entity using htmlspecialchars or htmlentities. Make sure you check the PHP manual to determine what is most appropriate to your data, as both functions operate slightly differently.

You can then reverse the encoding with htmlspecialchars_decode and html_entity_decode - again, check which is most appropriate for your data.

like image 35
akamike Avatar answered Oct 19 '22 22:10

akamike