PHP htmlentities() Functie
❮ Referentie voor PHP-tekenreeksen
Voorbeeld
Converteer enkele tekens naar HTML-entiteiten:
<?php
$str = '<a href="https://www.w3schools.com">Go to
w3schools.com</a>';
echo htmlentities($str);
?>
De HTML-uitvoer van de bovenstaande code is (Bron weergeven):
<a href="https://www.w3schools.com">Go to w3schools.com</a>
De browseruitvoer van de bovenstaande code is:
<a href="https://www.w3schools.com">Go to w3schools.com</a>
Definitie en gebruik
De functie htmlentities() converteert tekens naar HTML-entiteiten.
Tip: Gebruik de functie html_entity_decode() om HTML-entiteiten terug naar tekens te converteren .
Tip: Gebruik de functie get_html_translation_table() om de vertaaltabel te retourneren die wordt gebruikt door htmlentities().
Syntaxis
htmlentities(string,flags,character-set,double_encode)
Parameterwaarden
Parameter | Description |
---|---|
string | Required. Specifies the string to convert |
flags | Optional. Specifies how to handle quotes, invalid encoding and the used document type. The available quote styles are:
Invalid encoding:
Additional flags for specifying the used doctype:
|
character-set | Optional. A string that specifies which character-set to use. Allowed values are:
Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8. |
double_encode | Optional. A boolean value that specifies whether to encode existing html entities or not.
|
Technische details
Winstwaarde: | Retourneert de geconverteerde tekenreeks. Als de stringparameter echter ongeldige codering bevat, retourneert deze een lege string, tenzij de vlaggen ENT_IGNORE of ENT_SUBSTITUTE zijn ingesteld |
---|---|
PHP-versie: | 4+ |
Wijzigingslogboek: | PHP 5.6 - De standaardwaarde voor de tekensetparameter gewijzigd
in de waarde van de standaardtekenset (in configuratie). PHP 5.4 - De standaardwaarde voor de tekensetparameter gewijzigd in UTF-8. PHP 5.4 - ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 en ENT_XHTML toegevoegd PHP 5.3 - ENT_IGNORE constante toegevoegd. PHP 5.2.3 - De parameter double_encode toegevoegd . PHP 4.1 - De karaktersetparameter toegevoegd . |
Meer voorbeelden
Voorbeeld
Converteer enkele tekens naar HTML-entiteiten:
<?php
$str = "Albert Einstein said: 'E=MC²'";
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes
?>
De HTML-uitvoer van de bovenstaande code is (Bron weergeven):
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said:
'E=MC²'<br>
Albert Einstein said: 'E=MC²'
De browseruitvoer van de bovenstaande code is:
Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'
Albert
Einstein said: 'E=MC²'
Voorbeeld
Converteer enkele tekens naar HTML-entiteiten met behulp van de West-Europese tekenset:
<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo htmlentities($str, ENT_QUOTES, "UTF-8"); // Will only convert double quotes (not single quotes), and uses the character-set Western European
?>
De HTML-uitvoer van de bovenstaande code is (Bron weergeven):
<!DOCTYPE html>
<html>
<body>
My name is Øyvind Åsane. I'm Norwegian.
</body>
</html>
De browseruitvoer van de bovenstaande code is:
My name is Øyvind Åsane. I'm Norwegian.
❮ Referentie voor PHP-tekenreeksen