reverse htmlspecialchars

Ray S. picture Ray S. · Jun 29, 2012 · Viewed 31k times · Source

this may seem like a simple problem but I couldn't find it in the archives.

how does one reverse the effects of htmlspecialchars?

I tried something like this:

$trans_tbl = get_html_translation_table (HTML_ENTITIES);
$trans_tbl = array_flip ($trans_tbl);
$html = strtr ($html, $trans_tbl);

but it didn't work. is there a simple way to do this?

Answer

swapnesh picture swapnesh · Jun 29, 2012

Use htmlspecialchars_decode()

<?php
$str = "<p>this -&gt; &quot;</p>\n";

echo htmlspecialchars_decode($str);

// note that here the quotes aren't converted
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>

Reference - PHP Official Doc