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:
Encoding non validi:
Flags aggiuntivi per il tipo di documento utilizzato:
|
character-set |
Opzionale. Una stringa che specifica l'insieme di caratteri da utilizzare. Allowed values:
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.
|
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 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.