Funciones de strings
PHP Manual

htmlentities

(PHP 4, PHP 5)

htmlentitiesConvert all applicable characters to HTML entities

Descripción

string htmlentities ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

If you're wanting to decode instead (the reverse) you can use html_entity_decode().

Parámetros

string

The input string.

flags

Like htmlspecialchars(), the optional second flags parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being ENT_COMPAT, optionally combined with a fourth ENT_IGNORE (since PHP 5.3.0):

Available flags constants
Constant Name Description
ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.
ENT_IGNORE Silently discard invalid code unit sequences instead of returning an empty string. Added in PHP 5.3.0. This is provided for backwards compatibility; avoid using it as it may have security implications.

charset

Like htmlspecialchars(), it takes an optional third argument charset which defines character set used in conversion. Presently, the ISO-8859-1 character set is used as the default.

Los siguientes juegos de caracteres son soportados en PHP 4.3.0 y versiones posteriores.

Conjunto de Caracteres Soportados
Juego de caracteres Alias Descripción
ISO-8859-1 ISO8859-1 Europeo Occidental, Latin-1
ISO-8859-15 ISO8859-15 Europeo Occidental, Latin-9. Añade el signo de Euro, y letras del Francés y Finlandés que hacián falta en Latin-1(ISO-8859-1).
UTF-8   Multi-byte Unicode de 8-bits compatible con ASCII.
cp866 ibm866, 866 Juego de caracteres Cirílico específico de DOS. Este juego de caracteres está soportado en la versión 4.3.2.
cp1251 Windows-1251, win-1251, 1251 Juego de caracteres Cirílicos específico de Windows. Este juego de caracteres está soportado en la versíón 4.3.2.
cp1252 Windows-1252, 1252 Juego de caracteres específico de Windows para Europa Occidental.
KOI8-R koi8-ru, koi8r Ruso. Este juego de caracteres está soportado en la versión 4.3.2.
BIG5 950 Chino Tradicional, usado principalmente en Taiwán.
GB2312 936 Chino Simplificado, juego de caracteres estándar nacional.
BIG5-HKSCS   Big5 con extensiones de Hong Kong, Chino Tradicional.
Shift_JIS SJIS, 932 Japonés
EUC-JP EUCJP Japonés

Note: Cualquier otro juego de caracteres no es reconocido y en su lugar se utilizará ISO-8859-1.

double_encode

When double_encode is turned off PHP will not encode existing html entities. The default is to convert everything.

Valores devueltos

Returns the encoded string.

Historial de cambios

Versión Descripción
5.3.0 The constant ENT_IGNORE was added.
5.2.3 The double_encode parameter was added.
4.1.0 The charset parameter was added.
4.0.3 The flags parameter was added.

Ejemplos

Example #1 A htmlentities() example

<?php
$str 
"A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($strENT_QUOTES);
?>

Example #2 Usage of ENT_IGNORE

<?php
$str 
"\x8F!!!";

// Outputs an empty string
echo htmlspecialchars("\x8F!!!"ENT_QUOTES"UTF-8");

// Outputs "!!!"
echo htmlspecialchars("\x8F!!!"ENT_QUOTES ENT_IGNORE"UTF-8");
?>

Ver también


Funciones de strings
PHP Manual