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.