PHP htmlspecialchars() function

Example

Convert predefined characters "<" (less than) and ">" (greater than) to HTML entities:

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>

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

<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>

L'output del browser del codice sopra riportato è il seguente:

This is some <b>bold</b> text.

Esempio di esecuzione

Definition and usage

The htmlspecialchars() function converts predefined characters to HTML entities.

The predefined characters are:

  • & (ampersand) becomes &
  • " (double quote) becomes "
  • ' (single quote) becomes '
  • < (less than) becomes <
  • > (greater than) becomes >

Tip:To convert special HTML entities back to characters, use htmlspecialchars_decode() Function.

Syntax

htmlspecialchars(string,flags,character-set,double_encode)
Parameter Description
string Required. Specifies the string to be converted.
flags

Optional. Specifies how to handle quotes, invalid encoding, and which document type to use.

Available quote types:

  • ENT_COMPAT - Default. Encode double quotes only.
  • ENT_QUOTES - Encode both double and single quotes.
  • ENT_NOQUOTES - Do not encode any quotes.

Invalid encoding:

  • ENT_IGNORE - Ignore invalid encoding rather than returning an empty string from the function. It should be avoided as it may have security implications.
  • ENT_SUBSTITUTE - Substitute invalid encoding with a specified character that has the Unicode replacement character U+FFFD (UTF-8) or &#FFFD;, rather than returning an empty string.
  • ENT_DISALLOWED - Substitute invalid code points in the specified document type with the Unicode replacement character U+FFFD (UTF-8) or &#FFFD;.

Flags aggiuntivi per specificare il tipo di documento utilizzato:

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

Opzionale. Stringa che specifica il set di caratteri da utilizzare.

Valori permessi:

  • UTF-8 - Predefinito. ASCII compatibile 8-bit Unicode multibyte
  • ISO-8859-1 - Europa occidentale
  • ISO-8859-15 - Europa occidentale (aggiunge i simboli euro + lettere francesi e finlandesi perse in ISO-8859-1)
  • cp866 - Set di caratteri专用 Cyrillic per DOS
  • cp1251 - Set di caratteri专用 Cyrillic per Windows
  • cp1252 - Set di caratteri专用西欧 per Windows
  • KOI8-R - Russo
  • BIG5 - Cinese tradizionale, principalmente utilizzato a Taiwan
  • GB2312 - Cinese semplificato, set di caratteri di standard nazionale
  • BIG5-HKSCS - Big5 con estensioni di Hong Kong
  • Shift_JIS - Giapponese
  • EUC-JP - Giapponese
  • MacRoman - Set di caratteri utilizzato dal sistema operativo Mac

Note:Nelle versioni precedenti a PHP 5.4, i set di caratteri non riconosciuti vengono ignorati e sostituiti con ISO-8859-1. Dalla versione PHP 5.4 in poi, i set di caratteri non riconosciuti vengono ignorati e sostituiti con UTF-8.

double_encode

Opzionale. Valore booleano che specifica se codificare le entità HTML esistenti.

  • TRUE - Predefinito. Convertisce ogni entità.
  • FALSE - Non codifica le entità HTML esistenti.

Dettagli tecnici

Valore di ritorno:

Restituisce la stringa convertita.

Se string Se contiene codifiche non valide, restituisce una stringa vuota, a meno che non siano impostati i flag ENT_IGNORE o ENT_SUBSTITUTE.

Versione PHP: 4+
Registro delle modifiche:

In PHP 5,character-set Il valore predefinito del parametro è stato cambiato in UTF-8.

In PHP 5.4, sono state aggiunte: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 e ENT_XHTML.

In PHP 5.3, è stata aggiunta ENT_IGNORE.

In PHP 5.2.3, è stata aggiunta double_encode Parametro.

In PHP 4.1 è stata aggiunta character-set Parametro.

Più esempi

Esempio 1

Converti alcuni caratteri predefiniti in entità HTML:

<?php
$str = "Bill & 'Steve'";
echo htmlspecialchars($str, ENT_COMPAT); // Converti solo virgolette doppi
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES);// Converti virgolette doppi e singole
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Non convertire nessuna virgoletta
?>

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

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

L'output del browser del codice sopra riportato è il seguente:

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

Esempio di esecuzione

Esempio 2

Converti le virgolette doppie in entità HTML:

<?php
$str = 'I love "PHP".';
echo htmlspecialchars($str, ENT_QUOTES); // Converti virgolette doppi e singole
?>

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

<!DOCTYPE html>
<html>
<body>
Mi piace "PHP".
</body>
</html>

L'output del browser del codice sopra riportato è il seguente:

Mi piace "PHP".

Esempio di esecuzione