
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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;'