php htmlentities() function converts characters into HTML entities. This article introduces the basic usage and examples of the php htmlentities() function to coders. Coders in need can refer to it.
Definition and usage
htmlentities() function converts characters into HTML entities.
Tip: To convert HTML entities back to characters, use the html_entity_decode() function.
Tip: Please use the get_html_translation_table() function to return the translation table used by htmlentities().
Syntax
htmlentities(string,flags,character-set,double_encode)
##Technical details Example 1Convert characters to HTML entities:<?php $str = "Bill & 'Steve'"; echo htmlentities($str, ENT_COMPAT); // 只转换双引号 echo "<br>"; echo htmlentities($str, ENT_QUOTES); // 转换双引号和单引号 echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // 不转换任何引号 ?>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 2Convert some characters to HTML entities by using Western European character sets:
<?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.