Use when the value lands between HTML tags:
<p>HERE</p>,<div>HERE</div>,<li>HERE</li>.
escHtml is a thin wrapper around htmlspecialchars() configured with
ENT_QUOTES | ENT_SUBSTITUTE:
ENT_QUOTESescapes both single and double quotes, so the same output is safe inside an attribute should it ever be moved.ENT_SUBSTITUTEreplaces malformed UTF-8 withU+FFFDinstead of returning an empty string — failing safe, not silently.
use InitPHP\Escaper\Esc;
echo Esc::esc('<script>alert("xss")</script>');
// <script>alert("xss")</script>
echo Esc::esc("Tom & Jerry's adventure");
// Tom & Jerry's adventureThe escaper does not touch characters that are already safe in the HTML body context — letters, digits, accented characters, emoji, all pass through:
echo Esc::esc('Merhaba dünya 🚀');
// Merhaba dünya 🚀- Attribute values —
escHtmlis safe enough for quoted attributes but loses to unquoted attributes (a space ends the value). UseescHtmlAttrinstead. <script>blocks — HTML entities are not decoded inside a script. UseescJs.<style>blocks — same problem. UseescCss.- URLs in
href/src— escape the URL withescUrlfirst, then put the escaped URL throughescHtmlif needed.