htmlentities

(PHP 4, PHP 5, PHP 7, PHP 8)

htmlentitiesConvert all applicable characters to HTML entities

Descrição

htmlentities(
    string$string,
    int$flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401,
    ?string$encoding = null,
    bool$double_encode = true
): string

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. The get_html_translation_table() function can be used to return the translation table used dependent upon the provided flags constants.

If you want to decode instead (the reverse) you can use html_entity_decode().

Parâmetros

string

The input string.

flags

A bitmask of one or more of the following flags, which specify how to handle quotes, invalid code unit sequences and the used document type. The default is ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.

Available flags constants
Constant NameDescription
ENT_COMPATWill convert double-quotes and leave single-quotes alone.
ENT_QUOTESWill convert both double and single quotes.
ENT_NOQUOTESWill leave both double and single quotes unconverted.
ENT_IGNORE Silently discard invalid code unit sequences instead of returning an empty string. Using this flag is discouraged as it » may have security implications.
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
ENT_DISALLOWED Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is. This may be useful, for instance, to ensure the well-formedness of XML documents with embedded external content.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.
encoding

Um argumento opcional que define a codificação usada na conversão de caracteres.

Se omitido, encoding assume como padrão o valor da opção de configuração default_charset .

Embora este argumento seja tecnicamente opcional, especificar o valor correto para o código é altamente recomendável se a opção de configuração default_charset puder ser definida incorretamente para a entrada fornecida.

Os seguintes conjuntos de caracteres são suportados:

Conjuntos de caracteres suportados
Conjunto de caracteresApelidosDescrição
ISO-8859-1ISO8859-1 Western European, Latin-1.
ISO-8859-5ISO8859-5 Conjunto de caracteres cirílicos pouco usado (Latim/Cirílico).
ISO-8859-15ISO8859-15 Western European, Latin-9. Adiciona o símbolo do Euro, letras Francesas e Filandesas faltando no Latin-1 (ISO-8859-1).
UTF-8  Código de multi-byte 8-bit Unicode compatível com ASCII.
cp866ibm866, 866 Conjunto de caracteres do DOS específico para o Russo.
cp1251Windows-1251, win-1251, 1251 Conjunto de caracteres do Windows específico para o Russo.
cp1252Windows-1252, 1252 Conjunto de caracteres do Windows específico para a Europa Ocidental.
KOI8-Rkoi8-ru, koi8r Russo.
BIG5950 Chinês Tradicional, usado principalmente em Taiwan.
GB2312936 Chins Simplificado, conjunto de caracteres padrão nacional.
BIG5-HKSCS  Big5 com extenções de Hong Kong, Chinês Tradicional.
Shift_JISSJIS, SJIS-win, cp932, 932 Japonês
EUC-JPEUCJP, eucJP-win Japonês
MacRoman  Conjunto de caracteres que era usado pelo Mac OS.
''  Uma string vazia ativa a detecção a partir de codificação de script (multibyte Zend), conjunto padrão de caracteres e localidade atual (consulte nl_langinfo() e setlocale()), nesta ordem. Não recomendado.

Nota: Nenhum outro conjunto de caracteres é reconhecido. A codificação padrão será usada no lugar e um alerta será emitido.

double_encode

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

Valor Retornado

Returns the encoded string.

If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set.

Registro de Alterações

VersãoDescrição
8.1.0flags changed from ENT_COMPAT to ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401.
8.0.0encoding is nullable now.

Exemplos

Exemplo #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($str, ENT_QUOTES);
?>

Exemplo #2 Usage of ENT_IGNORE

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

// Outputs an empty string
echo htmlentities($str, ENT_QUOTES, "UTF-8");

// Outputs "!!!"
echo htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8");
?>

Veja Também

To Top