When a PHP page displays text containing markup characters, the intended output is often the literal text. Make that rendering rule explicit at the output boundary. This example creates a small helper for HTML text and tests characters that are easy to overlook: both quote types, an existing entity-looking sequence and an invalid UTF-8 byte.

The helper is for text content in an HTML document. It is not a URL policy, a JavaScript string encoder or an HTML sanitizer for intentionally permitted markup.

Prerequisites

Use PHP 8.3 or another supported PHP 8 release. Save the program as escape.php. It runs with the command-line interpreter and requires no web server or extensions beyond the standard runtime.

Complete example

<?php
declare(strict_types=1);
function text(string $value): string {
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8', true);
}
function check(bool $ok): void {
    if (!$ok) throw new RuntimeException('check failed');
}
$input = '<b title="x">Tom & Ada</b>';
$output = text($input);
check($output === '&lt;b title=&quot;x&quot;&gt;Tom &amp; Ada&lt;/b&gt;');
check(text("'") === '&apos;');
check(text('&lt;') === '&amp;lt;');
check(bin2hex(text("A\x80B")) === '41efbfbd42');
echo $output, "\n";
echo "text, quotes, entity and invalid UTF-8 checks passed\n";

Run and expected output

php escape.php

The first line below is the actual entity text printed by the CLI, shown inside an escaped code block:

&lt;b title=&quot;x&quot;&gt;Tom &amp; Ada&lt;/b&gt;
text, quotes, entity and invalid UTF-8 checks passed

Understand the rendering policy

The input contains a complete-looking tag, but the output represents its angle brackets as text. Ampersands are encoded too, so an existing sequence such as &lt; is displayed literally instead of being treated as an already approved entity. That behavior follows from the explicit final argument of true.

The chosen flags encode both quote types, use HTML5 entity rules and replace malformed UTF-8 with the replacement character. The byte-level test checks that replacement directly. It avoids depending on how a terminal happens to draw invalid input or the replacement glyph.

The checks throw exceptions directly rather than relying on PHP assertion settings. A deployment that disables assertions therefore still runs these example checks.

Edge cases and troubleshooting

Keep stored application text in its original form and escape it when rendering. Encoding once during storage and again during output can produce visible double encoding. The helper intentionally encodes entity-looking input; disabling that behavior should be a reviewed content-contract decision rather than a quick fix for mixed stored data.

Use UTF-8 consistently in the surrounding HTML response and template files. Escaping a string under one character-set assumption cannot repair a page emitted under a different one. If malformed input must be rejected rather than displayed with replacement characters, add validation at ingestion and test that separate policy.

A safe text representation does not make an arbitrary link destination acceptable. Validate schemes and destinations separately when building links. Likewise, do not insert this helper’s output into inline JavaScript or CSS and assume the HTML text contract covers those grammars.

The CLI execution verifies exact output bytes and helper behavior. Browser rendering, template integration and HTTP response headers were not exercised.

References and related tutorials

The official htmlspecialchars manual documents its flags, charset argument and entity behavior. Browse PHP tutorials for related output handling.