Funzione PHP htmlentities()

Esempio

Convert characters to HTML entities:

<?php
$str = "<? W3S?h????>";
echo htmlentities($str);
?>

L'output HTML del codice sopra è il seguente (vedere il codice sorgente):

<!DOCTYPE html>
<html>
<body>
<© W3Sçh°°¦§>
</body>
</html>

Output del browser del codice sopra:

<? W3S?h????>

Definizione e uso

La funzione htmlentities() converte i caratteri in codici HTML.

Suggerimento:Per convertire i codici HTML in caratteri, utilizzare html_entity_decode() funzione.

Suggerimento:Utilizzare get_html_translation_table() Funzione per restituire la tabella di traduzione utilizzata da htmlentities().

Sintassi

htmlentities(string,flags,character-set,double_encode)
Parametro Descrizione
string Obbligatorio. Specifica la stringa da convertire.
flags

Opzionale. Specifica come trattare le virgolette, i codici di encoding non validi e quale tipo di documento utilizzare.

Tipi di virgolette disponibili:

  • ENT_COMPAT - Predefinito. Codifica solo virgolette doppie.
  • ENT_QUOTES - Codifica virgolette doppie e singole.
  • ENT_NOQUOTES - Non codifica nessuna virgoletta.

Encoding non validi:

  • ENT_IGNORE - Ignora i codici di encoding non validi, invece di restituire una stringa vuota. Evitare di utilizzarlo, poiché potrebbe avere implicazioni sulla sicurezza.
  • ENT_SUBSTITUTE - Sostituisce i codici di encoding non validi con un carattere specificato che contiene il carattere di sostituzione Unicode U+FFFD (UTF-8) o &#FFFD;, invece di restituire una stringa vuota.
  • ENT_DISALLOWED - Sostituisce i punteggi di codice non validi specificati nel tipo di documento con il carattere di sostituzione Unicode U+FFFD (UTF-8) o &#FFFD;.

Flags aggiuntivi per il tipo di documento utilizzato:

  • ENT_HTML401 - Predefinito. Trattato come codice HTML 4.01.
  • ENT_HTML5 - Trattato come codice HTML 5.
  • ENT_XML1 - Trattato come codice XML 1.
  • ENT_XHTML - Trattato come codice XHTML.
character-set

Opzionale. Una stringa che specifica l'insieme di caratteri da utilizzare.

Allowed values:

  • UTF-8 - Default. ASCII compatible 8-bit Unicode
  • ISO-8859-1 - Western Europe
  • ISO-8859-15 - Western Europe (includes euro symbol + missing French and Finnish letters in ISO-8859-1)
  • cp866 - Cyrillic character set for DOS
  • cp1251 - Cyrillic character set for Windows
  • cp1252 - Western European character set for Windows
  • KOI8-R - Russian
  • BIG5 - Traditional Chinese, mainly used in Taiwan
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5-HKSCS - Big5 with Hong Kong extension
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character set used by the Mac operating system

Note:In versions of PHP before 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. Starting from PHP 5.4, unrecognized character sets will be ignored and replaced by UTF-8.

double_encode

Optional. Boolean value, specifies whether to encode existing HTML entities.

  • TRUE - Default. Converts each entity.
  • FALSE - Will not encode existing HTML entities.

Technical details

Return value:

Returns the converted string.

If string If it contains invalid encoding, it returns an empty string unless ENT_IGNORE or ENT_SUBSTITUTE flags are 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); // Non convertirà nessuna virgoletta
?>

L'output HTML del codice sopra è il seguente (vedere il codice sorgente):

<!DOCTYPE html>
<html>
<body>
Bill & 'Steve'<br>
Bill & 'Tarzan'<br>
Bill & 'Steve'
</body>
</html>

Output del browser del codice sopra:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

Esempio di esecuzione

Esempio 2

Convertendo alcuni caratteri in entity HTML utilizzando il set di caratteri europeo occidentale:

<?php
$str = "My name is Øyvind Øsane. I'm Norwegian.";
echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); 
//Convertirà solo le virgolette doppi (non le virgolette semplici) e utilizza il set di caratteri europeo occidentale
?>

L'output HTML del codice sopra è il seguente (vedere il codice sorgente):

<!DOCTYPE html>
<html>
<body>
Il mio nome è Øyvind Øsane. Sono norvegese.
</body>
</html>

Output del browser del codice sopra:

Il mio nome è Øyvind Øsane. Sono norvegese.

Esempio di esecuzione