zendframework/zend-escaper
Zend Escaper is a PHP library for context-aware escaping to help prevent XSS. Escape HTML, HTML attributes, JavaScript, CSS, and URLs with reliable encoders, making it easy to safely output untrusted data in templates and web responses.
composer require zendframework/zend-escaper\Zend\Escaper\Escaper — it's stateless and thread-safe, so one instance can be reused (e.g., via dependency injection or a singleton)$escaper = new \Zend\Escaper\Escaper('utf-8');
echo $escaper->escapeHtml($userInput); // Safely outputs as HTML text content
escapeHtml() for HTML element content (not attributes)escapeHtmlAttr() for HTML attributesescapeJs() for JavaScript string literalsescapeCss() for CSS property valuesescapeUrl() for URL query parameters or full URLs (with validation)escapeHtmlLower() / escapeHtmlUpper() for less common cases (rarely needed)Escaper as a singleton in a service provider; create a e() helper wrapping $escaper->escapeHtml() if replacing Blade’s default {{ }}escapeHtml() in custom Twig filters$变量, $request->input(), or DB results are safeEscaper instance rather than creating many — performance is negligible, but reuse avoids duplicationescapeHtml() for attributes — use escapeHtmlAttr() or htmlspecialchars() with ENT_QUOTES manually. escapeHtml() intentionally does not escape quotes, assuming HTML content context'utf-8' (or your app’s encoding) in the constructor — default may break non-ASCII contentlaminas/laminas-escaper instead (same API, maintained by Laminas Project); zendframework/zend-escaper will continue to work but won’t receive updates& instead of &) usually means double-escaping — trace whether data was already escaped before reaching the escaperEscaper to add domain-specific escaping (e.g., escapeMarkdown()), but avoid overriding core methods — extend only with new methodsHow can I help you explore Laravel packages today?