I am having a small issue with displaying text that has been converted with htmlspecialchars. In the db I have this:
<b&amp;gt;Testing:</b&amp;gt;<br&amp;gt;<ol&amp;gt;<li&amp;gt;it&#039;s a test<br&amp;gt;</li&amp;gt;</ol&amp;gt;

When I use:

echo htmlspecialchars_decode($variable);

The page displays:
<b>Testing:</b><br><ol><li>it's a test<br></li></ol>

I am not sure why this is happening. Any help is appreciated. Thank you

Edited by ProfessorPC

3 Years
Discussion Span
Last Post by ProfessorPC

Not sure if the DB encoded html makes much sense - seems incomplete with ampersands all over the place. Looks double encoded. What output were you expecting?

echo htmlspecialchars_decode('&amp;');

Should give &

echo htmlspecialchars_decode("<b&amp;gt;Testing:</b&amp;gt;<br&amp;gt;<ol&amp;gt;<li&amp;gt;it&#039;s a test<br&amp;gt;</li&amp;gt;</ol&amp;gt;");

Gives me...

<b&gt;Testing:</b&gt;<br&gt;<ol&gt;<li&gt;it&#039;s a test<br&gt;</li&gt;</ol&gt;

in Browser View Source.

If you run htmlspecialchars_decode twice...

echo htmlspecialchars_decode(htmlspecialchars_decode("<b&amp;gt;Testing:</b&amp;gt;<br&amp;gt;<ol&amp;gt;<li&amp;gt;it&#039;s a test<br&amp;gt;</li&amp;gt;</ol&amp;gt;"));

I get this...

<b>Testing:</b><br><ol><li>it&#039;s a test<br></li></ol>

Which is closer to what you probably want.

Edited by diafol


I am getting <b>Testing:</b><br><ol><li>it&#039;s a test<br></li></ol>, but that is exactly what is displaying on my page. It's not showing bold text it is showing the tags.


If it's showing the tags on-screen then the output is still encoded. Have a look at View Source in your browser.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.