40

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?

Ray S.
  • 1,192
  • 3
  • 15
  • 27
  • 1
    Read the manual, it will tell you all functions that have something to do with the one you are looking at in the 'see also' section [this is that section for `htmlspecialchars()`](http://php.net/manual/en/function.htmlspecialchars.php#refsect1-function.htmlspecialchars-seealso) – Manuel Jul 31 '13 at 09:54
  • 4
    This question appears to be off-topic because it can be found in the [manual](http://php.net/manual/en/function.htmlspecialchars-decode.php). – Ja͢ck Jul 31 '13 at 09:58

4 Answers4

80

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

swapnesh
  • 26,318
  • 22
  • 94
  • 126
4

You need htmlspecialchars_decode(). See PHP docu on this.

$html = htmlspecialchars_decode( $html, ENT_NOQUOTES );
Sirko
  • 72,589
  • 19
  • 149
  • 183
4

example :

echo htmlspecialchars_decode(htmlspecialchars('your "strange" text with characters like !"/$%?&*'))

it will echo : your "strange" text with characters like !"/$%?&*

this is an example of encode/decode. it works.

0

From what I understood, you need htmlspecialchars_decode - Docu

Christoph
  • 50,121
  • 21
  • 99
  • 128
hjpotter92
  • 78,589
  • 36
  • 144
  • 183