Happy Coder
Happy Coder

Reputation: 4682

Show HTML special characters as is

I need to display a few item names as it is stored in database. It is working fine except when the name contains HTML special characters. For eg: if the name is like &lt;ItemName&gt; it is showed as <ItemName> when echo it using PHP. How can I prevent this. Also if the name is stored in DB as <ItemName> it should show like that only. When I tried to use htmlentities(), it is showing the & as &amp; and that isn't what I need to show. How this can be fixed ?

Also I am using Highcharts and it has the item names as labels. So the name <ItemName> (if with tags), needs to be converted to htmlentities() in order to display it correctly. Otherwise, it will not show the label.

Upvotes: 1

Views: 118

Answers (1)

Thamilhan
Thamilhan

Reputation: 13303

Just use htmlspecialchars function

<?php
echo(htmlspecialchars('<ItemName>something</ItemName>'));

or as in your case:

<?php
echo(htmlspecialchars('&lt;ItemName&gt;something&lt;/ItemName&gt;'));

Upvotes: 2

Related Questions