Skip to content

Core\Html::escape

Makes a text safe to show on an HTML page.

Core\Html::escape writes the five characters &, <, >, " and ' as character references. A browser shows them as text and never reads them as tags. It always escapes all five, so the result is safe inside an element and inside an attribute value. A direction control (a Unicode character that changes the direction of the text) that is never closed is replaced with �. So it cannot reverse the text after it.

The result is a Core\Html\Markup value, not a string. A page writes a Markup value exactly as it is. So a page does not escape the result a second time. Text from a visitor is tainted (it came from outside the program), and escape is how such a text goes into a page.

To write a whole attribute, use Core\Html::attributes. It writes the name and the quoted value. It also checks URL attributes such as href, and leaves out a link that starts with javascript:. escape does not check URLs.

Good to know: a text that already contains &amp; is escaped again, and becomes &amp;amp;.

Parameters

  • $textstringThe text to write into an HTML document. It is shown as text, not read as HTML.

Return value

Returns Core\Html\Markup. A Core\Html\Markup with the escaped text. It is safe in element content and in an attribute value with either kind of quotes. It is not a string, so the page does not escape it a second time. toSource returns its text as a string. Text without the five characters and without an unterminated control character does not change. The five characters are always escaped, and no option turns this off. Input that already looks like a reference is escaped again, because &amp; in the input is the text &amp;.

Examples

A name with special characters

<?nvs
// Shows how to write a text into an HTML page, so that it shows as text and not as tags.
string $name = 'Tom & Jerry <admin>';
// `Core\Html::escape` writes `&`, `<` and `>` as character references.
// This prints `Tom &amp; Jerry &lt;admin&gt;`.
echo Core\Html::escape($name), "\n";
// A text with none of the five characters stays the same.
echo Core\Html::escape('Ada Lovelace'), "\n";
Output
Tom &amp; Jerry &lt;admin&gt;
Ada Lovelace

A text inside an attribute

<?nvs
// Shows how to put a text inside an HTML attribute, where a quote would end the value too early.
string $title = "Say \"hello\" and it's done";
// Both quotes are escaped: `"` becomes `&quot;` and `'` becomes `&#39;`.
// `as Core\Html\Markup` turns the tag parts into `Markup`. They are HTML that you wrote, so they are not escaped.
var $link = ("<a title=\"" as Core\Html\Markup)
+ Core\Html::escape($title)
+ ("\">Help</a>" as Core\Html\Markup);
echo $link, "\n";
Output
<a title="Say &quot;hello&quot; and it&#39;s done">Help</a>

Comments from visitors

<?nvs
// Shows how to build an HTML list from comments that visitors wrote.
array<string> $comments = ['Great post!', '<script>alert(1)</script>', 'Fish & chips'];
// Each comment is read as `tainted`, which means it came from outside the program.
// A tainted text can only go into a page after `Core\Html::escape`.
array<Core\Html\Markup> $items = [];
foreach ($comments as tainted string $comment) {
$items[] = ('<li>' as Core\Html\Markup) + Core\Html::escape($comment) + ('</li>' as Core\Html\Markup);
}
// `Core\Html::join` puts the items together. The script tag is printed as text.
echo ("<ul>\n" as Core\Html\Markup) + Core\Html::join($items, "\n" as Core\Html\Markup) + ("\n</ul>" as Core\Html\Markup), "\n";
Output
<ul>
<li>Great post!</li>
<li>&lt;script&gt;alert(1)&lt;/script&gt;</li>
<li>Fish &amp; chips</li>
</ul>