PHP String htmlentities() Function
The PHP String htmlentities() function is used to convert all the applicable characters to HTML entities. It is identical to htmlspecialchars() in every manner except that, like htmlentities(), it converts all characters with HTML character entity equivalents into these entities.
The get_html_translation_table() method returns the translation table that was utilized based on the flags constants provided. If you want to decode instead (in reverse), use html_entity_decode().
Syntax
Below is the syntax of the PHP String htmlentities() function −
string htmlentities ( $string, $flags, $character-set, $double_encode)
Parameters
Here are the parameters of the htmlentities() function −
| Sr.No | Parameters & Description |
|---|---|
| 1 |
$string It contains the information about input string |
| 2 |
$flags It contains the information about flags |
| 3 |
$character-set It is an optional argument defining the encoding used when converting characters. |
| 4 |
$double_encode When it is turned off PHP will not encode existing html entities. The default value is to convert everything. |
Available Flags Constants
Here are the list of available flags constants −
| Constant Name | Description |
|---|---|
| ENT_COMPAT | Converts double-quotes and leave single-quotes alone. |
| ENT_QUOTES | Converts both double and single quotes. |
| ENT_NOQUOTES | Leaves both double and single quotes unconverted. |
| ENT_SUBSTITUTE | Used to replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or � (otherwise) instead of returning an empty string. |
| ENT_HTML401 | Handles code as HTML 4.01. |
| ENT_XML1 | Handles code as XML 1. |
| ENT_XHTML | Handles code as XHTML. |
| ENT_HTML5 | Handles code as HTML 5. |
Supported Character sets
The following character sets are supported in with this function −
| Charset | Aliases | Description |
|---|---|---|
| ISO-8859-1 | ISO8859-1 | Western European, Latin-1. |
| ISO-8859-5 | ISO8859-5 | Little used cyrillic charset (Latin/Cyrillic). |
| ISO-8859-15 | ISO8859-15 | Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1). |
| UTF-8 | ASCII compatible multi-byte 8-bit Unicode. | |
| cp866 | ibm866, 866 | DOS-specific Cyrillic charset. |
| cp1251 | Windows-1251, win-1251, 1251 | Windows-specific Cyrillic charset. |
| cp1252 | Windows-1252, 1252 | Windows specific charset for Western European. |
| KOI8-R | koi8-ru, koi8r | Russian. |
| BIG5 | 950 | Traditional Chinese, mainly used in Taiwan. |
| GB2312 | 936 | Simplified Chinese, national standard character set. |
| BIG5-HKSCS | Big5 with Hong Kong extensions, Traditional Chinese. | |
| Shift_JIS | SJIS, SJIS-win, cp932, 932 | Japanese. |
| EUC-JP | EUCJP, eucJP-win | Japanese. |
| MacRoman | Charset that was used by Mac OS. |
Return Value
The htmlentities() function returns the transformed string. If the string parameter has incorrect encoding, it will return an empty string, unless the ENT_IGNORE or ENT_SUBSTITUTE flags are specified.
PHP Version
First introduced in core PHP 4, the htmlentities() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
First we will show you the basic example of the PHP String htmlentities() function to convert all the applicable characters to HTML entities.
<?php
// Define a string here
$str = "PHP Function htmlentities";
// Use htmlentities() function here
echo htmlentities($str);
echo htmlentities($str, ENT_QUOTES);
?>
Output
Here is the outcome of the following code −
PHP Function htmlentitiesPHP Function htmlentities
Example 2
In the below PHP code we will use the htmlentities() function and convert the given string to html entities.
<?php $str = "The 'quote' is <b>bold</b>"; echo htmlentities($str); echo "\n\n"; echo htmlentities($str, ENT_COMPAT); ?>
Output
This will generate the below output (View Source) −
The & #039;quote& #039; is & lt;b& gt;bold& lt;/b& gt; The 'quote' is & lt;b& gt;bold& lt;/b& gt;
Example 3
Now in the below code we will use htmlentities() function and also use ENT_IGNORE flag as an argument and see the result how this function works.
<?php $str = "\x8F!!!"; // Outputs an empty string echo htmlentities($str, ENT_QUOTES, "UTF-8"); // Outputs "!!!" echo htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8"); ?>
Output
This will create the below output −
!!!
Example 4
In the following example, we are using the htmlentities() function to convert some characters to HTML entities.
<?php $str = "Albert Einstein Invented: 'E=MC'"; // only converts double quotes echo htmlentities($str, ENT_COMPAT); echo "\n"; // Converts double and single quotes echo htmlentities($str, ENT_QUOTES); echo "\n"; // Does not convert any quotes echo htmlentities($str, ENT_NOQUOTES); ?>
Output
Following is the output of the above code (View Source) −
Albert Einstein Invented: 'E=MC& sup2;' Albert Einstein Invented: & #039;E=MC& sup2;& #039; Albert Einstein Invented: 'E=MC& sup2;'