[PHP] Escape special HTML characters

Created on November 30, 2026

htmlspecialchars() escapes characters that have special meaning in HTML. This example uses UTF-8 and passes ENT_QUOTES to escape both single and double quotes.

<?php

$text = '<a href="https://example.com/?name=Tom&age=20">Tom\'s page</a>';

echo htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
&lt;a href=&quot;https://example.com/?name=Tom&amp;age=20&quot;&gt;Tom&#039;s page&lt;/a&gt;
PHP