PHP htmlentities() Function
Topic: PHP String ReferencePrev|Next
Description
The htmlentities()
function converts all applicable characters to HTML entities.
This function typically reverses the effect of html_entity_decode()
function.
The following table summarizes the technical details of this function.
Return Value: | Returns the encoded string. If the input string contains an invalid code sequence within the given charset it will return an empty string, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set. |
---|---|
Version: | PHP 4+ |
Syntax
The basic syntax of the htmlentities()
function is given with:
The following example shows the htmlentities()
function in action.
Example
Run this code »<?php
// Sample string
$str = "It's an <b>\"amazing\"</b> story.";
// Encoding the string
$encoded_str = htmlentities($str);
echo $encoded_str;
?>
The output of the above example will be (view source to get an idea):
However, in the browser you will see something like this:
Note: The htmlentities()
is identical to htmlspecialchars()
in all ways, except that htmlspecialchars()
only replaces &
, <
, and >
, with option for single and double quotes. But htmlentities()
replaces all characters which can be represented by HTML character entity.
Tip: You can use the get_html_translation_table()
function to return the translation table that is used internally for the htmlspecialchars()
and htmlentities()
functions.
Parameters
The htmlentities()
function accepts the following parameters.
Parameter | Description |
---|---|
string | Required. Specifies the string to encode. |
flags |
Optional. Specifies how to handle quotes, invalid code sequences and which document type to use. You can specify one or more of the following flags. The available flags constants for handling quotes are:
The available flags constants for handling invalid code sequences are:
The available flags constants for specifying the document types are:
The default value for this parameter is |
charset |
Optional. Specifies which character set to use. Supported charsets are:
If this parameter is omitted, it defaults to the value of the |
double_encode | Optional. A Boolean value which specifies whether to encode existing html entities or not. Possible values are true and false . Default value is true which convert everything. |
More Examples
Here're some more examples showing how htmlentities()
function actually works:
The following example demonstrates the handling of single and double quotes using this function.
Example
Run this code »<?php
// Sample string
$str = "I'll \"leave\" tomorrow.";
// Convert only double quotes
$a = htmlentities($str, ENT_COMPAT);
echo $a; /* I'll "leave" tomorrow. */
// Converts both double and single quotes
$b = htmlentities($str, ENT_QUOTES);
echo $b; /* I'll "leave" tomorrow. */
// Does not convert any quotes
$c = htmlentities($str, ENT_NOQUOTES);
echo $c; /* I'll "leave" tomorrow. */
?>
However, in the browser you will always see the string I'll "leave" tomorrow.
View source (right-click and select View Page Source) of the example output to see the actual encoded string.