PHP htmlspecialchars_decode() Function
In this chapter you will learn:
- Definition for PHP htmlspecialchars_decode() Function
- Syntax for PHP htmlspecialchars_decode() Function
- Parameter for PHP htmlspecialchars_decode() Function
- Return for PHP htmlspecialchars_decode() Function
- Example - Convert the predefined HTML entities
- Example - Convert some predefined HTML entities to characters
Definition
The htmlspecialchars_decode() function converts some predefined HTML entities to characters.
HTML entities that will be decoded are:
- & becomes & (ampersand)
- " becomes " (double quote)
- ' becomes ' (single quote)
- < becomes < (less than)
- > becomes > (greater than)
The htmlspecialchars_decode() function is the opposite of htmlspecialchars().
Syntax
PHP htmlspecialchars_decode() Function has the following syntax.
htmlspecialchars_decode(string,flags)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to decode |
flags | Optional. | How to handle quotes and which document type to use. |
The available quote styles are:
- ENT_COMPAT - Default. Decodes only double quotes
- ENT_QUOTES - Decodes double and single quotes
- ENT_NOQUOTES - Does not decode any quotes
- ENT_HTML401 - Default. Handle code as HTML 4.01
- ENT_HTML5 - Handle code as HTML 5
- ENT_XML1 - Handle code as XML 1
- ENT_XHTML - Handle code as XHTML
Return
PHP htmlspecialchars_decode() Function returns the converted string.
Example 1
Convert the predefined HTML entities "<" (less than) and ">" (greater than) to characters:
<?php
$str = "java2s.com <b>bold</b> text.";
echo htmlspecialchars_decode($str);
?>
The code above generates the following result.
Example 2
Convert some predefined HTML entities to characters:
<?php/* jav a2s . c o m*/
$str = "Java & '.com'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "\n";
echo htmlspecialchars_decode($str, ENT_QUOTES); // Converts double and single quotes
echo "\n";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // Does not convert any quotes
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP join() Function
- Syntax for PHP join() Function
- Parameter for PHP join() Function
- Return for PHP join() Function
- Example - Join array elements with a string
- Example - Separate the array elements with different characters
Home » PHP Tutorial » PHP String Functions