HTML Encode / Decode

Convert special characters into HTML entities and back.

What is HTML encoding

Converts characters with special meaning in HTML — like <, >, & and quotes — into entities (&lt;, &gt;, &amp;) so they display as literal text instead of being interpreted as code.

When to use it

Essential for displaying source code inside a web page, or to prevent user-typed text from being interpreted as HTML/script (basic XSS prevention).

Input and output example

The text <script>alert(1)</script> becomes &lt;script&gt;alert(1)&lt;/script&gt; and appears on the page as plain text instead of running. Double quotes become &quot; and a single quote is often written &#39;; both matter when the text lands inside an attribute such as value="...".

Why you see &amp;amp; on the page

That means double encoding: the ampersand of an existing entity was encoded again. When encoding, the ampersand must be handled first; if it came last, the entities produced earlier would be corrupted.

Context matters

HTML entities suit element text and attribute values. Inside a script block, a URL or CSS, escaping follows different rules, so HTML encoding alone is not enough there.

The entity &nbsp; stands for a non-breaking space and &#169; for the © symbol. Numeric entities work for any Unicode character.

Frequently asked questions

Why do I need to encode HTML?

To display characters like < and > as literal text instead of having the browser interpret them as part of the page's code.

Does this protect against XSS attacks?

It helps as a basic layer, but doesn't replace proper validation and sanitization on the backend.