phpday 2027 - Call for Papers

htmlspecialchars_decode

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

htmlspecialchars_decode — Converte le entità HTML speciali in caratteri

Descrizione

function htmlspecialchars_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string

Questa funzione è l'opposto di htmlspecialchars(). Converte le entità HTML speciali in caratteri.

Le entità convertite sono: &, " (quando ENT_NOQUOTES non è impostato), ' (quando ENT_QUOTES è impostato), < e >.

Elenco dei parametri

string

La stringa da decodificare.

flags

Una maschera di bit di uno o più dei seguenti flag, che specificano come gestire gli apici e quale tipo di documento utilizzare. Il valore predefinito è ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

Costanti disponibili per flags
Nome della costante Descrizione
ENT_COMPAT Converte le virgolette doppie e lascia invariati gli apici singoli.
ENT_QUOTES Converte sia le virgolette doppie sia gli apici singoli.
ENT_NOQUOTES Non converte né le virgolette doppie né gli apici singoli.
ENT_SUBSTITUTE Sostituisce le sequenze di unità di codice non valide con il carattere di sostituzione Unicode U+FFFD (UTF-8) oppure con � (negli altri casi) invece di restituire una stringa vuota.
ENT_HTML401 Gestisce il codice come HTML 4.01.
ENT_XML1 Gestisce il codice come XML 1.
ENT_XHTML Gestisce il codice come XHTML.
ENT_HTML5 Gestisce il codice come HTML 5.

Valori restituiti

Restituisce la stringa decodificata.

Log delle modifiche

Versione Descrizione
8.1.0 Il valore predefinito di flags è cambiato da ENT_COMPAT a ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

Esempi

Example #1 Esempio di htmlspecialchars_decode()

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

echo htmlspecialchars_decode($str);

// si noti che qui gli apici non vengono convertiti
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>

Il precedente esempio visualizzerà:

<p>this -> "</p>
<p>this -> &quot;</p>

Vedere anche:

+add a note

User Contributed Notes 3 notes

up
2
thomas at xci[ignore_this]teit dot commm
18 years ago
The example for "htmlspecialchars_decode()" below sadly does not work for all PHP4 versions.

Quote from the PHP manual:
"get_html_translation_table() will return the translation table that is used internally for htmlspecialchars() and htmlentities()."

But it does NOT! At least not for PHP version 4.4.2.
This was already reported in a bug report (http://bugs.php.net/bug.php?id=25927), but it was marked as BOGUS.

Proof:
  Code:
--------------------
<?php
    var_dump(get_html_translation_table(HTML_SPECIALCHARS,ENT_QUOTES));
    var_dump(htmlspecialchars('\'',ENT_QUOTES));
?>
--------------------

  Output:
--------------------
array
  '"' => '&quot;'
  ''' => '&#39;'
  '<' => '&lt;'
  '>' => '&gt;'
  '&' => '&amp;'

'&#039;'
--------------------

This comment now is not to report this bug again (though I really believe it is one), but to complete the example and warn people of this pitfall.

To make sure your htmlspecialchars_decode fake for PHP4 works, you should do something like this:

<?php
    function htmlspecialchars_decode($string,$style=ENT_COMPAT)
    {
        $translation = array_flip(get_html_translation_table(HTML_SPECIALCHARS,$style));
        if($style === ENT_QUOTES){ $translation['&#039;'] = '\''; }
        return strtr($string,$translation);
    }
?>

Br, Thomas
up
-1
Anonymous
20 years ago
This should be the best way to do it.
(Reposted because the other one seems a bit slower and because those who used the code under called it htmlspecialchars_decode_php4)

<?php

if ( !function_exists('htmlspecialchars_decode') )
{
    function htmlspecialchars_decode($text)
    {
        return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
    }
}

?>
up
-2
or-k at or-k dot com
21 years ago
that works also with &auml; and &quot; and so on.
get_html_translation_table(HTML_ENTITIES) => offers more characters than HTML_SPECIALCHARS

function htmlspecialchars_decode_PHP4($uSTR)
{
 return strtr($uSTR, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
}
To Top