PHP htmlentities() Function
PHP htmlentities() Function
The htmlentities() function in PHP converts all characters to HTML entities.
Syntax
htmlentities(string,flags,character-set,double_encode);
Parameter
string(required)- This parameter specifies the string to convert.
flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The available flag constants are as follows:
- ENT_COMPAT- Table contains entities only for double-quotes
- ENT_QUOTES- The Table contains entities for both double and single quotes
- ENT_NOQUOTES- Table contains entities neither for single quotes nor for double quotes.
- ENT_HTML401- Table specifically for HTML 4.01.
- ENT_XML1- Table for XML 1.
- ENT_XHTML- Table for XHTML.
- ENT_HTML5- Table for HTML 5.
character(optional)- It specifies the character set.
double_encode(optional)- This parameter represents a boolean value that states whether to encode existing html entities or not.
Return
This function returns the encoded string.
Example 1
TutorialandExample'; // It will convert htmlentities echo htmlentities( $str ); //printing the value ?>
Output
<a href="https://www.tutorialandexample.com/">TutorialandExample</a>
Example 2
&<Example>"; echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes echo "\n"; echo htmlentities($str, ENT_QUOTES); // will convert the double and single quotes echo "\n"; echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes ?>
Output
<Tutorial><Example> <Tutorial>&<Example> <Tutorial>&<Example>
Example 3
Output
My name is &amar. I'm from 'India'.