简体   繁体   中英

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.

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;'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM