HTML Entity Encoding: When & and < Are Needed

Last updated: · 3 min read

When you write user-supplied text into an HTML page, characters such as &, < and > can be mistaken for markup and break the layout or open the door to injection problems. Converting those characters into HTML entities makes the browser display them as plain text. The HTML Entity Encoder turns special characters into &amp;, &lt;, &gt;, &quot; and &#39;. If you choose the broader scope, it also converts every non-ASCII character such as é, © or € into a named or numeric entity. The conversion runs in your browser.

Formula

1. Read the text one character at a time.
2. Replace '&' with '&amp;', '<' with '&lt;' and '>' with '&gt;'.
3. Replace the double quote with '&quot;' and the apostrophe with '&#39;'.
4. If the scope is 'all', write every non-ASCII character as a named entity (for example &euro; or &copy;) where one exists, otherwise as a numeric entity (&#NNN;).
5. Keep all other characters unchanged and join the results.

This is not a mathematical formula but a character mapping algorithm. The named entity table follows the WHATWG HTML Living Standard. Because the ampersand is encoded first, the new entities produced in the process are not encoded a second time.

What the inputs mean

Text
The text to encode. It might be a user comment, a product name or the value of a form field. Line breaks and spaces are kept as they are.
Scope
The 'Only & < > " '' option encodes just the characters that matter in HTML; the 'Also all non-ASCII characters' option additionally converts characters such as é, © and € into entities.

Worked example: Making a priced text safe for HTML

  1. Input: Price: €5 & "tax" <b> with the scope 'Also all non-ASCII characters'.
  2. The ampersand becomes &amp;, so the other entities in the output are not encoded twice.
  3. The double quotes become &quot; and the opening angle bracket of <b> becomes &lt;, so the tag is shown as text instead of being interpreted.
  4. The euro sign is non-ASCII and becomes the named entity &euro;.

Result: Output: Price: &euro;5 &amp; &quot;tax&quot; &lt;b&gt;. Written into HTML, this displays exactly as the original text in the browser.

🔣 HTML Entity Encoder

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Encoding stops the text from being read as markup, but the right rules depend on where the text ends up. Inside an attribute value, quotes must be encoded. If the text goes into JavaScript or a URL, HTML encoding alone is not enough, so use the encoding that matches that context. For security, always use the output in the correct context. This is general information.

Common mistakes

  • Thinking that only &, < and > need encoding; inside attribute values the quotes must be encoded too.
  • Choosing the broader scope without need; on UTF-8 pages accented characters can be written directly, and converting them makes the source harder to read.
  • Encoding text that is already encoded; encoding &amp; again produces &amp;amp;, which displays as the literal text &amp;.
  • Assuming HTML encoding is enough inside JavaScript or a URL; each context has its own encoding rules.
  • Finding &#39; for the apostrophe odd and reverting it; it is a standard entity that browsers display correctly.

Frequently asked questions

Which characters must always be encoded?

The characters &, <, >, double quote and apostrophe. They can conflict with the HTML structure, so encoding them is recommended.

Do I need to encode accented characters?

On pages that use UTF-8 and declare it correctly, usually not. The narrower scope is normally enough.

Is the text sent to a server?

No. Encoding runs in your browser and the text is not transmitted anywhere.

Can I reverse the encoding?

Yes, with the HTML Entity Decoder, which converts entities back into characters.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice