Función PHP htmlentities()
❮ Referencia de cadenas de PHP
Ejemplo
Convierta algunos caracteres en entidades HTML:
<?php
$str = '<a href="https://www.w3schools.com">Go to
w3schools.com</a>';
echo htmlentities($str);
?>
La salida HTML del código anterior será (Ver código fuente):
<a href="https://www.w3schools.com">Go to w3schools.com</a>
La salida del navegador del código anterior será:
<a href="https://www.w3schools.com">Go to w3schools.com</a>
Definición y uso
La función htmlentities() convierte caracteres en entidades HTML.
Sugerencia: para volver a convertir entidades HTML en caracteres, utilice la función html_entity_decode() .
Sugerencia: utilice la función get_html_translation_table() para devolver la tabla de traducción utilizada por htmlentities().
Sintaxis
htmlentities(string,flags,character-set,double_encode)
Valores paramétricos
Parameter | Description |
---|---|
string | Required. Specifies the string to convert |
flags | Optional. Specifies how to handle quotes, invalid encoding and the used document type. The available quote styles are:
Invalid encoding:
Additional flags for specifying the used doctype:
|
character-set | Optional. A string that specifies which character-set to use. Allowed values are:
Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8. |
double_encode | Optional. A boolean value that specifies whether to encode existing html entities or not.
|
Detalles técnicos
Valor devuelto: | Devuelve la cadena convertida. Sin embargo, si el parámetro de cadena contiene una codificación no válida, devolverá una cadena vacía, a menos que se establezcan los indicadores ENT_IGNORE o ENT_SUBSTITUTE |
---|---|
Versión PHP: | 4+ |
Registro de cambios: | PHP 5.6: cambió el valor predeterminado para el parámetro del juego de caracteres
al valor del juego de caracteres predeterminado (en configuración). PHP 5.4: se cambió el valor predeterminado para el parámetro de conjunto de caracteres a UTF-8. PHP 5.4: se agregaron ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 y ENT_XHTML PHP 5.3: se agregó la constante ENT_IGNORE. PHP 5.2.3: se agregó el parámetro double_encode . PHP 4.1: se agregó el parámetro de conjunto de caracteres . |
Más ejemplos
Ejemplo
Convierta algunos caracteres en entidades HTML:
<?php
$str = "Albert Einstein said: 'E=MC²'";
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes
?>
La salida HTML del código anterior será (Ver código fuente):
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said:
'E=MC²'<br>
Albert Einstein said: 'E=MC²'
La salida del navegador del código anterior será:
Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'
Albert
Einstein said: 'E=MC²'
Ejemplo
Convierta algunos caracteres en entidades HTML utilizando el conjunto de caracteres de Europa Occidental:
<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo htmlentities($str, ENT_QUOTES, "UTF-8"); // Will only convert double quotes (not single quotes), and uses the character-set Western European
?>
La salida HTML del código anterior será (Ver código fuente):
<!DOCTYPE html>
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
La salida del navegador del código anterior será:
My name is Øyvind Åsane. I'm Norwegian.
❮ Referencia de cadenas de PHP