In this article, we will see what htmlentities() & htmlspecialchars() Function is used for & also understand their implementation through the examples. Show htmlentities() Function: The htmlentities() function is an inbuilt function in PHP that is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entities. Syntax: string htmlentities( $string, $flags, $encoding, $double_encode ) Parameters value: This function accepts four parameters as mentioned above and described below:
Return Values: This function returns the string which has been encoded. Example: This example uses the htmlentities() function to transform all characters which are applicable to HTML entities. PHPOutput: <a href="https://www.geeksforgeeks.org">GeeksforGeeks</a> htmlspecialchars() Function: The htmlspecialchars() function is an inbuilt function in PHP which is used to convert all predefined characters to HTML entities. Syntax: string htmlspecialchars( $string, $flags, $encoding, $double_encode ) Parameter value:
Return Values: This function returns the converted string. If there is an invalid input string then an empty string will be returned. Example: This example uses the htmlspecialchars() function to convert all predefined characters to HTML entities. PHP
Output: "geeksforgeeks.org" Go to GeeksforGeeks Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities. ❮ PHP String Reference ExampleConvert the predefined HTML entities "<" (less than) and ">" (greater than) to characters: <?php The HTML output of the code above will be (View Source): <!DOCTYPE
html> The browser output of the code above will be: This is some bold text. Definition and UsageThe htmlspecialchars_decode() function converts some predefined HTML entities to characters. HTML entities that will be decoded are:
The htmlspecialchars_decode() function is the opposite of htmlspecialchars(). Syntaxhtmlspecialchars_decode(string,flags) Parameter Values
Technical Details
More ExamplesExampleConvert some predefined HTML entities to characters: <?php The HTML output of the code above will be (View Source): <!DOCTYPE html> The browser output of the code above will be: Jane & 'Tarzan' ExampleConvert the predefined HTML entities to double quotes: <?php The HTML output of the code above will be (View Source): <!DOCTYPE html> The browser output of the code above will be: I love "PHP". ❮ PHP String Reference What is HTML entities () function?htmlentities() Function: The htmlentities() function is an inbuilt function in PHP that is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entities. Syntax: string htmlentities( $string, $flags, $encoding, $double_encode )
What is HTML entities function in PHP?The htmlentities() function converts characters to HTML entities. Tip: To convert HTML entities back to characters, use the html_entity_decode() function. Tip: Use the get_html_translation_table() function to return the translation table used by htmlentities().
How do I allow special characters in PHP?Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode() function.. & (ampersand) becomes &. " (double quote) becomes ". ' (single quote) becomes '. < (less than) becomes <. > (greater than) becomes >. What is the difference between HTML entities and Htmlspecialchars in PHP?The only difference between htmlspecialchars() and htmlentities() function is that htmlspecialchars() function converts the special characters to HTML entities, whereas htmlentities() function converts all the applicable characters to html entities.
|