API Index — Namespaces, Packages, Reports, Indices
Escape
in package
Context-specific output escaping for plain PHP templates (#509).
Pick the method for the context the value lands in:
| Context | Method |
|---|---|
| HTML text and quoted attribute values | Escape::html() / e() |
| One URL component (path segment, query) | Escape::url() |
A whole URL in href / src |
Escape::safeUrl() |
A value inside <script> or data-* JSON |
Escape::js() |
| Trusted markup you built yourself | Escape::raw() |
html() encodes & < > " ', so the same call is safe for text and for
attribute values in single or double quotes. It is NOT safe for unquoted
attributes, inside <script>/<style>, or for event-handler attributes.
Invalid UTF-8 is replaced with U+FFFD rather than returning an empty string.
Table of Contents
Constants
- HTML_FLAGS : mixed = ENT_QUOTES | ENT_SUBSTITUTE
- JS_FLAGS : mixed = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | J...
- SAFE_SCHEMES : mixed = ['http', 'https', 'mailto', 'tel']
- URL schemes
safeUrl()lets through; anything else becomes#.
Methods
- html() : string
- Escape for HTML text or a quoted attribute value.
- js() : string
- Encode a value as a JavaScript literal (JSON) that is safe inside a
<script>block or an HTML attribute:< > & ' "are \u-escaped, so the output can't close the script element or the attribute. - raw() : SafeHtml
- Mark a string as trusted markup so {@see html()} / {@see e()} emit it as-is. Only wrap markup you built from escaped parts or a trusted source.
- safeUrl() : string
- HTML-escape a whole URL for
href/src, refusing dangerous schemes. - url() : string
- Percent-encode one URL component (RFC 3986). Use for a path segment or a query value, not for a whole URL. The result is also HTML-safe.
- toString() : string
Constants
HTML_FLAGS
private
mixed
HTML_FLAGS
= ENT_QUOTES | ENT_SUBSTITUTE
JS_FLAGS
private
mixed
JS_FLAGS
= JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR
SAFE_SCHEMES
URL schemes safeUrl() lets through; anything else becomes #.
private
mixed
SAFE_SCHEMES
= ['http', 'https', 'mailto', 'tel']
Methods
html()
Escape for HTML text or a quoted attribute value.
public
static html(mixed $value) : string
null becomes an empty string. A SafeHtml value is returned
unchanged — it is already markup.
Parameters
- $value : mixed
Return values
stringjs()
Encode a value as a JavaScript literal (JSON) that is safe inside a
<script> block or an HTML attribute: < > & ' " are \u-escaped, so
the output can't close the script element or the attribute.
public
static js(mixed $value) : string
<script>const user = <?= Escape::js($user) ?>;</script>
<div data-config='<?= Escape::js($config) ?>'></div>
Parameters
- $value : mixed
Return values
stringraw()
Mark a string as trusted markup so {@see html()} / {@see e()} emit it as-is. Only wrap markup you built from escaped parts or a trusted source.
public
static raw(string $html) : SafeHtml
Parameters
- $html : string
Return values
SafeHtmlsafeUrl()
HTML-escape a whole URL for href / src, refusing dangerous schemes.
public
static safeUrl(mixed $value) : string
Relative URLs (/path, ?q=1, #frag, page.php) and the schemes
http, https, mailto and tel pass through. Anything else — javascript:,
data:, vbscript:, including case or whitespace tricks — becomes #.
Parameters
- $value : mixed
Return values
stringurl()
Percent-encode one URL component (RFC 3986). Use for a path segment or a query value, not for a whole URL. The result is also HTML-safe.
public
static url(mixed $value) : string
Parameters
- $value : mixed
Return values
stringtoString()
private
static toString(mixed $value) : string
Parameters
- $value : mixed