PHP htmlentities() functie
Voorbeeld
Convert characters to HTML entities:
<?php $str = "<? W3S?h????>"; echo htmlentities($str); ?>
Hier is de HTML-uitvoer van de bovenstaande code (bekijk de broncode):
<!DOCTYPE html> <html> <body> <© W3Sçh°°¦§> </body> </html>
Hier is de browser-uitvoer van de bovenstaande code:
<? W3S?h????>
Definitie en gebruik
de functie htmlentities() om tekens om te zetten naar HTML-entiteiten.
Tips:Om HTML-entiteiten om te zetten naar tekens, gebruik dan html_entity_decode() functie.
Tips:Gebruik get_html_translation_table() De functie om de vertalingstabel te retourneren die door htmlentities() wordt gebruikt.
Syntax
htmlentities(string,flags,character-set,double_encode)
Parameters | Beschrijving |
---|---|
string | Verplicht. Specificeer de te converteren string. |
flags |
Optioneel. Specificeer hoe aanhalingstekens, ongeldige codering en het gebruikte documenttype worden afgehandeld. Beschikbare aanhalingstekentypen:
Ongeldige codering:
Extra flags voor het gespecificeerde documenttype:
|
character-set |
Optioneel. Een string die de te gebruiken tekenset specificeert. Allowed values:
Note:In versions of PHP before 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. From PHP 5.4 onwards, 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: |
Return the converted string. If string If it contains invalid encoding, it will return 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 is 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); // Geen aanhalingstekens converteren ?>
Hier is de HTML-uitvoer van de bovenstaande code (bekijk de broncode):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Tarzan'<br> Bill & 'Steve' </body> </html>
Hier is de browser-uitvoer van de bovenstaande code:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
Voorbeeld 2
Door gebruik te maken van het West-Europese character-set, worden enkele karakters omgezet naar HTML-entiteiten:
<?php $str = "Mijn naam is Øyvind Øsane. Ik ben Noors."; echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); // Alleen dubbele aanhalingstekens (niet enkele aanhalingstekens) worden geconverteerd, en gebruikt het character-set Westers Europees ?>
Hier is de HTML-uitvoer van de bovenstaande code (bekijk de broncode):
<!DOCTYPE html> <html> <body> Mijn naam is Øyvind Øsane. Ik ben Noors. </body> </html>
Hier is de browser-uitvoer van de bovenstaande code:
Mijn naam is Øyvind Øsane. Ik ben Noors.