
Wikis & learning
Render user text in an HTML template
Plain-text data can contain characters that have special meaning in HTML. Encode the value for the place where the template will display it.
Plain-text data can contain characters that have special meaning in HTML. Encode the value for the place where the template will display it.
Walkthrough
Use htmlspecialchars at the output boundary rather than altering stored text. The example writes HTML source for a paragraph. A browser would display the original ampersand and angle brackets as text.
PHP example
<?php
$title = 'Tools & notes <spring>';
$html = '<p>' . htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</p>';
echo $html;Expected output
<p>Tools & notes <spring></p>Change and check
Replace the project title with a sentence containing double quotes. Inspect the output HTML and then inspect its rendered appearance in a browser.
Discussion
Sign in to participate.



