Home PHP String Functions htmlentities() PHP htmlentities() Function Definition In PHP, htmlentities() converts applicable characters to HTML entities so browsers show them as text.
The string is required. Flags and encoding are optional. Prefer htmlspecialchars() when you only need the common special characters.
Syntax Call htmlentities on the string. Start with this line.
$safe = htmlentities($text, ENT_QUOTES, 'UTF-8'); Call What it does htmlentities("<b>Hi</b>")Encodes angle brackets and more. htmlspecialchars($text, ENT_QUOTES, 'UTF-8')Encodes a smaller special set.
Always set ENT_QUOTES and UTF-8 when escaping for HTML attributes and text.
Library declarations htmlentities(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string Parameters Parameter Description string Required. The input text to encode. flags Optional. Quote and entity handling flags. encoding Optional. Character encoding. Prefer UTF-8.
Return Value Return Value Description string A string with characters converted to HTML entities.
PHP Examples 1. Basic encode 2. Quotes + UTF-8 3. Round-trip Example 1 — Encode Tags and Symbols Turn markup characters and © into entities for safe HTML text.
<?php
echo htmlentities("<b>Hi</b> & ©", ENT_QUOTES, "UTF-8");
// <b>Hi</b> & © <b>Hi</b> & © How It Works Specials. <, >, and & become <, >, &.Symbols. © becomes © (entity coverage beyond htmlspecialchars).Flags. ENT_QUOTES prepares quote encoding when quotes appear.UTF-8. Passing "UTF-8" keeps multi-byte characters correct.Example 2 — Encode Quotes with ENT_QUOTES Both single and double quotes become entities when ENT_QUOTES is set.
<?php
echo htmlentities("It's \"ok\"", ENT_QUOTES, "UTF-8");
// It's "ok" How It Works Single quote. ' becomes ' with ENT_QUOTES.Double quote. " becomes ".Why it matters. Safer when placing text inside HTML attributes.Defaults. Pass flags explicitly—PHP 8.1+ changed default flag combinations.Example 3 — Encode, Then Decode Matching flags and UTF-8 restore the original string.
<?php
$original = '<a href="x">Hi</a>';
$encoded = htmlentities($original, ENT_QUOTES, "UTF-8");
$decoded = html_entity_decode($encoded, ENT_QUOTES, "UTF-8");
echo ($decoded === $original) ? "same" : "diff";
// same How It Works Encode. Markup and quotes become entities.Decode. html_entity_decode() reverses them.Same flags. ENT_QUOTES on both sides keeps quotes consistent.Strict match. === confirms an exact round-trip.Frequently Asked Questions What does PHP htmlentities() do? htmlentities() converts characters that have HTML entity equivalents into those entities. It encodes specials like &, <, and > plus many symbols and accented letters (such as © and é) so they display safely as text in HTML.
What is the syntax for htmlentities()? string htmlentities(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true). Pass the input, ENT_* flags, UTF-8 encoding, and whether to encode existing entities again.
What is the difference between htmlentities() and htmlspecialchars()? htmlspecialchars() encodes only the core specials (&, <, >, quotes). htmlentities() encodes every character that has an HTML entity equivalent—including ©, €, and many accented letters. For typical XSS-safe page output, htmlspecialchars() is usually enough.
When should I use htmlentities() instead of htmlspecialchars()? Use htmlentities() when you need full entity encoding of symbols and accented characters. For standard HTML escaping against XSS, htmlspecialchars() is the common choice.
What does the double_encode parameter do? When true (default), existing entities like © are encoded again to &copy;. When false, already-encoded entities are left unchanged—useful when input may already contain entities.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion