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.
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:
Invalid encoding:
Flags aggiuntivi per specificare il tipo di documento utilizzato:
|
character-set |
Opzionale. Stringa che specifica il set di caratteri da utilizzare. Valori permessi:
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.
|
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 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".