HTML Tags and Entities Explained (And Why Stripping Them Is Trickier Than It Looks)

HTML tags and entities are two different mechanisms for representing formatted text, and understanding both explains why "just removing the tags" doesn't always give clean output.

A tag marks the start and end of an element

A tag like <p> or <strong> wraps content between an opening and closing pair to tell a browser how to treat it β€” a paragraph, bold text, a link. The tags themselves carry no visible text; they are instructions, not content.

Entities represent characters that would otherwise break the markup

Because characters like < and & have special meaning in HTML, they are written as entities instead β€” &lt; for <, &amp; for &, &nbsp; for a non-breaking space. A correct converter has to turn these back into literal characters, not just delete tags.

Block elements imply line breaks that inline elements do not

Elements like <div>, <p>, and <li> are block-level and visually start a new line, while <span>, <a>, and <strong> are inline and sit within a line of text. Simply deleting all tags without adding a line break where block elements were joins separate paragraphs into one run-on line.

Common reasons to strip tags from pasted content

Pasting rich text copied from a webpage into a plain-text field, a spreadsheet cell, or a system that cannot render markup often carries invisible HTML formatting along with it, which shows up as garbled text or stray symbols unless it is cleanly removed first.

Removing visible tags is not the same as making content safe

A regex that strips <tag> patterns can be fooled by malformed, nested, or deliberately obfuscated markup, and stripping display formatting is a different job from sanitizing input against injected scripts β€” a security context needs a proper sanitization library, not a simple tag-removal pass.

Why entities exist in the first place

HTML uses a handful of characters, like < and &, as syntax β€” the moment a browser sees a literal < in raw text, it assumes a tag is starting. Entities let a document safely contain those characters as visible text instead of markup instructions, which is why a correct extraction of "plain text" has to decode them back to normal characters.

Where naive tag-stripping breaks down

Real-world HTML is rarely as clean as a textbook example β€” unclosed tags, tags split across copy-paste boundaries, and comments or script blocks containing angle brackets that aren't actual tags all trip up a simple find-and-delete approach. A proper HTML parser, rather than a pattern match, handles these edge cases correctly.

Frequently Asked Questions

If I strip the tags from a link, do I lose the URL?

Yes, in most simple approaches β€” the visible link text remains, but the href destination the tag was pointing to disappears along with the tag itself, since that information was never part of the visible text to begin with.

Is stripping HTML tags the same as sanitizing HTML for security?

No. Stripping tags focuses on producing clean plain text for display or storage, while sanitization focuses on preventing malicious code execution and generally needs a dedicated, well-tested library rather than a simple tag-removal script.