PHP htmlentities() function
Example
Convert characters to HTML entities:
<?php $str = "<? W3S?h????>"; echo htmlentities($str); ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> <© W3Sçh°°¦§> </body> </html>
Browser output of the above code:
<? W3S?h????>
Definition and Usage
The htmlentities() function converts characters to HTML entities.
Tip:To convert HTML entities back to characters, use html_entity_decode() function.
Tip:Please use get_html_translation_table() Function to return the translation table used by htmlentities().
Syntax
htmlentities(string,flags,character-set,double_encode)
Parameter | Description |
---|---|
string | Required. Specifies the string to be converted. |
flags |
Optional. Specifies how quotes, invalid encoding, and which document type are to be handled. Available quote types:
Invalid encoding:
Additional flags for specifying the document type used:
|
character-set |
Optional. A string that specifies the character set to be used. Allowed values:
Note:In versions of PHP before 5.4, unrecognized character sets are ignored and replaced by ISO-8859-1. From PHP 5.4 onwards, unrecognized character sets are 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 the string contains invalid encoding, an empty string is returned 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 and single quotes echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // Do not convert any quotes ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Tarzan'<br> Bill & 'Steve' </body> </html>
Browser output of the above code:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
Example 2
Convert some characters to HTML entities using the Western European character set:
<?php $str = "My name is Øyvind Øsane. I'm Norwegian."; echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); // Will only convert double quotes (not single quotes), and uses the character-set Western European ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> My name is Øyvind Øsane. I'm Norwegian. </body> </html>
Browser output of the above code:
My name is Øyvind Øsane. I'm Norwegian.