Tác dụng của hàm html-entity-decode()
The html_entity_decode()
function converts HTML entities to their corresponding characters.
This function typically reverses the effect of htmlentities()
function.
The following table summarizes the technical details of this function.
Return Value: | Returns the decoded string. |
---|---|
Version: | PHP 4.3.0+ |
Syntax
The basic syntax of the html_entity_decode()
function is given with:
The following example shows the html_entity_decode()
function in action.
Ví dụ
<?php
// Sample string
$str = "It's an <b>amazing</b> story.";
// Encoding the string
$encoded_str = htmlentities($str);
echo $encoded_str . "<br>";
// Decoding the string
$decoded_str = html_entity_decode($encoded_str);
echo $decoded_str;
?>
Parameters
The html_entity_decode()
function accepts the following parameters.
Parameter | Description |
---|---|
string | Required. Specifies the string to decode. |
flags |
Optional. Specifies how to handle quotes and which document type to use. The available flags constants for handling quotes 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 |
More Examples
Here're some more examples showing how html_entity_decode()
function actually works:
The following example demonstrates the handling of single and double quotes using this function.
Ví dụ
<?php
// Sample string
$str = "I'll \"leave\" tomorrow.";
// Encoding the string
$encoded_str = htmlentities($str, ENT_QUOTES);
echo $encoded_str; /* I'll "leave" tomorrow. */
// Converts only double-quotes
$a = html_entity_decode($encoded_str);
echo $a; /* I'll "leave" tomorrow. */
// Converts both double and single quotes
$b = html_entity_decode($encoded_str, ENT_QUOTES);
echo $b; /* 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 converted string.