(PHP 5 >= 5.1.0, PHP 7, PHP 8)
htmlspecialchars_decode — Convierte las entidades HTML especiales en caracteres
$string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string Esta función es la opuesta a htmlspecialchars() . Convierte las entidades HTML especiales en caracteres.
Las entidades convertidas son: &,
" (cuando ENT_NOQUOTES no está activado),
' (cuando ENT_QUOTES está activado),
< y >.
stringLa string a decodificar
flags
Una máscara de uno o varios flags siguientes,
que especifican cómo deben ser gestionadas las comillas
y qué tipo de documento utilizar. Por omisión, es
ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.
| Nombre de la constante | Descripción |
|---|---|
ENT_COMPAT |
Convertirá las comillas y dejará las apóstrofes. |
ENT_QUOTES |
Convertirá las comillas y los apóstrofes. |
ENT_NOQUOTES |
Dejará las comillas y los apóstrofes sin convertir. |
ENT_SUBSTITUTE |
Reemplaza las secuencias de código no válidas con un carácter de reemplazo Unicode U+FFFD (UTF-8) o &#FFFD; (en otro caso) en lugar de devolver una cadena vacía. |
ENT_HTML401 |
Gestiona el código como HTML 4.01. |
ENT_XML1 |
Gestiona el código como XML 1. |
ENT_XHTML |
Gestiona el código como XHTML. |
ENT_HTML5 |
Gestiona el código como HTML 5. |
Devuelve la cadena de caracteres decodificada.
| Versión | Descripción |
|---|---|
| 8.1.0 |
flags cambió de ENT_COMPAT a
ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 .
|
Ejemplo #1 Ejemplo con htmlspecialchars_decode()
<?php
$str = "<p>this -> "</p>\n";
echo htmlspecialchars_decode($str);
// note aquí que las comillas no están convertidas
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>El ejemplo anterior mostrará:
<p>this -> "</p> <p>this -> "</p>
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
'"' => '"'
''' => '''
'<' => '<'
'>' => '>'
'&' => '&'
'''
--------------------
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['''] = '\''; }
return strtr($string,$translation);
}
?>
Br, ThomasThis 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)));
}
}
?>that works also with ä and " 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)));
}