Función PHP htmlentities()
Ejemplo
Convert characters to HTML entities:
<?php $str = "<? W3S?h????>"; echo htmlentities($str); ?>
La salida HTML del código anterior es como sigue (ver código fuente):
<!DOCTYPE html> <html> <body> <© W3Sçh°°¦§> </body> </html>
La salida del navegador del código anterior es:
<? W3S?h????>
Definición y uso
La función htmlentities() convierte caracteres en entidades HTML.
Consejo:Para convertir entidades HTML en caracteres, utilice html_entity_decode() Función.
Consejo:Utilice get_html_translation_table() Función para devolver la tabla de traducción utilizada por htmlentities().
Sintaxis
htmlentities(string,flags,character-set,double_encode)
Parámetros | Descripción |
---|---|
string | Requerido. Especifica la cadena a convertir. |
flags |
Opcional. Especifica cómo se deben manejar las comillas, la codificación no válida y qué tipo de documento se debe usar. Tipos de comillas disponibles:
Codificación no válida:
Flags adicionales para especificar el tipo de documento utilizado:
|
character-set |
Opcional. Una cadena que especifica el conjunto de caracteres a usar. Allowed values:
Note:In versions of PHP before 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. From PHP 5.4 onwards, unrecognized character sets will be ignored and replaced by UTF-8. |
double_encode |
Optional. Boolean value, specifies whether to encode existing HTML entities.
|
Technical details
Return value: |
Return the converted string. If string If it contains invalid encoding, an empty string is returned unless the ENT_IGNORE or ENT_SUBSTITUTE flag is set. |
PHP version: | 4+ |
Update log: |
In PHP 5,character-set The default value of the parameter has been changed to UTF-8. In PHP 5.4, new features were added: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML. In PHP 5.3, a new feature was added: ENT_IGNORE. In PHP 5.2.3, a new feature was added: double_encode Parameters. In PHP 4.1, a new feature was added: character-set Parameters. |
More examples
Example 1
Convert characters to HTML entities:
<?php $str = "Bill & 'Steve'"; echo htmlentities($str, ENT_COMPAT); // Only convert double quotes echo "<br>"; echo htmlentities($str, ENT_QUOTES); // Convert double quotes and single quotes echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // No convertir ninguna comilla ?>
La salida HTML del código anterior es como sigue (ver código fuente):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Tarzan'<br> Bill & 'Steve' </body> </html>
La salida del navegador del código anterior es:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
Ejemplo 2
Al usar el conjunto de caracteres de Europa Occidental, convierte algunos caracteres en entidades HTML:
<?php $str = "My name is Øyvind Øsane. I'm Norwegian."; echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); // Solo convertirá comillas dobles (no comillas simples) y utiliza el conjunto de caracteres de Europa Occidental ?>
La salida HTML del código anterior es como sigue (ver código fuente):
<!DOCTYPE html> <html> <body> Mi nombre es Øyvind Øsane. Soy noruego. </body> </html>
La salida del navegador del código anterior es:
Mi nombre es Øyvind Øsane. Soy noruego.