Php show html tags in string

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

asked May 5, 2010 at 19:20

You can HTML encode the string via htmlspecialchars:

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

answered May 5, 2010 at 19:23

D'Arcy RittichD'Arcy Rittich

162k38 gold badges283 silver badges279 bronze badges

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.

answered May 5, 2010 at 19:24

akamikeakamike

2,12013 silver badges16 bronze badges

2

You could always just replace < with &lt; and > with &gt;, yeah?

answered May 5, 2010 at 19:21

ChrisChris

27.1k23 gold badges122 silver badges221 bronze badges

2

You can use textarea tag as following:

<textarea rows="5" cols="20"><?php echo "<b>Text<b>"; ?></textarea>

answered May 11, 2018 at 7:41

Php show html tags in string

Not the answer you're looking for? Browse other questions tagged php html replace tags strip or ask your own question.

How do I display HTML tags as plain text in PHP?

If you want to use PHP to show HTML tags as text, then, you can use the function <code>htmlspecialchars()</code> to escape < and > characters.

How do you display HTML code in text?

Once you encode your HTML, you will want to preserve the formatting and indentation of the HTML. To do this, use the <pre> tag. This tag is used on on preformatted text, hence the name.

How do I strip HTML tags in PHP?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe.

How do I display HTML code without executing?

Use HTML Special Character Codes var myCode = "<b>This is not bold</b>"; $('span#code-span'). text(myCode); Using text instead of html will cause tags to be rendered exposed instead of being executed.