A common use for html_entity_decode is processing data without rebuilding the operation manually. Converts HTML character references to characters, subject to flags and chosen encoding.
What Is html_entity_decode in PHP?
Converts HTML character references to characters, subject to flags and chosen encoding.
Syntax:
html_entity_decode(?string $string, ?int $flags = ..., ??string $encoding = ...) : string
Parameters and Return Value
$string— A required string input for this operation. Verify the allowed values in the examples and context below.$flags— An optional int input for this operation. Verify the allowed values in the examples and context below.$encoding— An optional ?string input for this operation. Verify the allowed values in the examples and context below.
Declared return type: string. Distinguish false, an empty result, and exceptions where the API permits them.
Example 1: Basic Usage
This example demonstrates the main operation with a small, controlled input.
<?php
echo html_entity_decode("Tom & Jerry", ENT_QUOTES | ENT_HTML5, "UTF-8");
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Example 2: Another Input
This example changes the input or surrounding setup to illustrate another behavior.
<?php
echo html_entity_decode("<example>", ENT_QUOTES | ENT_HTML5, "UTF-8");
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Practical Example
This application-style example shows a self-contained usage pattern and checks its result.
<?php
$storedText = "Bread & Butter";
echo html_entity_decode($storedText, ENT_QUOTES | ENT_HTML5, "UTF-8");
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Important Behavior and Edge Cases
- Decoding entities does not sanitize content; escape again when inserting untrusted output into HTML.
- Use the intended input type and handle empty or malformed values explicitly.
- Where this API requires an extension, version, or file, document that dependency before using it in a real application.
Comparison With Related Functions
| Function or Method | When to Use It |
|---|---|
html_entity_decode | Converts HTML character references to characters, subject to flags and chosen encoding. |
htmlspecialchars | Processes structured data using htmlspecialchars(). |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using this API without checking the required input or extension | Decoding entities does not sanitize content; escape again when inserting untrusted output into HTML. |
| Treating malformed input as a successful result | Handle invalid values and documented failure results explicitly. |
| Using this operation for the wrong data representation | Choose the matching data format and compare its result to the required output. |
Key Takeaways
- Converts HTML character references to characters, subject to flags and chosen encoding.
- Decoding entities does not sanitize content; escape again when inserting untrusted output into HTML.
- Test both the expected case and at least one failure or empty-input case before deployment.
Converts HTML character references to characters, subject to flags and chosen encoding. Use it when the operation fits your input, output and validation requirements.