htmlentities()
function converts characters into 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()
.
Convert characters to HTML entities:
<?php $str = "<? W3S?h????>" ; echo htmlentities ( $str ) ; ?>
The HTML output of the above code is as follows (see the source code):
< ! DOCTYPE html > < html > < body > < ? W3S ?h°°|§ > < / body > < / html >
The browser output of the above code:
<? W3S ? h ?? ? ?>
Try it yourself
Convert characters to HTML entities:
<?php $str = "Bill & 'Steve'" ; echo htmlentities ( $str , ENT_COMPAT ) ; // Convert only double quotes echo "<br>" ; echo htmlentities ( $str , ENT_QUOTES ) ; // Convert double and single quotes echo "<br>" ; echo htmlentities ( $str , ENT_NOQUOTES ) ; // No quotation marks are converted ?>
The HTML output of the above code is as follows (see the source code):
< ! DOCTYPE html > < html > < body > Bill & 'Steve' < br > Bill & & #039;Tarzan'<br> Bill & 'Steve' < / body > < / html >
The browser output of the above code:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
Try it yourself
By using the Western European character set, convert some characters into HTML entities:
<?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 (see the source code):
< ! DOCTYPE html > < html > < body > My name is ?yvind ?sane . I 'm Norwegian . < / body > < / html >
The browser output of the above code:
My name is ?yvind ?sane. I'm Norwegian.
Try it yourself
htmlentities ( string , flags , character - set , double_encode )
parameter | describe |
---|---|
string | Required. Specifies the string to be converted. |
flags |
Optional. Specifies how to deal with quotes, invalid encodings, and which document type to use. Available quote types:
Invalid encoding:
Additional flags for the document type used:
|
character-set |
Optional. A string that specifies the character set to be used. Allowed values:
Note: In versions prior to PHP 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. Since PHP 5.4, unrecognized character sets will be ignored and replaced by UTF-8. |
double_encode |
Optional. Boolean value, specifying whether to encode an existing HTML entity.
|