How to remove html special chars?

How to remove html special chars?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Difference Between Stripping Tags and Decoding Entities

When building an RSS feed, the goal is often to present clean, readable text within <content:encoded> blocks or <description> tags. If you use PHP's strip_tags() function to remove HTML markup, you might find that while the structural elements like <b>, <p>, and <img> disappear, the entity references remain intact. You are left with strings containing &nbsp;, &amp;, and &copy; instead of actual spaces, ampersands, or copyright symbols.

This happens because strip_tags() operates strictly on the syntax level. It parses the string looking for opening and closing tags defined in a whitelist (or blacklist) and removes them. It does not interpret the content within those removed tags, nor does it process entity references that exist as text nodes. To your browser or RSS aggregator, &nbsp; is still valid XML/HTML text, which renders correctly in most modern readers but looks like raw code if you are inspecting the feed source or exporting to a format that expects plain UTF-8 characters.

To resolve this, you need to move from stripping syntax to decoding entities. The standard solution in PHP is the html_entity_decode() function. This function translates HTML entities back into their corresponding characters.

Here is how you combine these two steps effectively:

$input = '<b>Hello &amp; World</b> &nbsp;';

// Step 1: Remove all HTML tags (strip_tags defaults to stripping everything)
$stripped = strip_tags($input); 

// At this point, $stripped is: "Hello &amp; World &nbsp;"

// Step 2: Decode the remaining entities
$coded = html_entity_decode($stripped, ENT_QUOTES | ENT_HTML5, 'UTF-8');

// Result: "Hello & World "

The resulting string contains actual ampersand characters and non-breaking spaces rather than their escaped representations. This is crucial for RSS feeds because many parsers expect raw UTF-8 text without entity escapes to ensure consistent rendering across different clients.

Handling Special Cases and Flags

When using html_entity_decode(), the flags you pass determine how aggressively the function decodes data. The default behavior uses ENT_NOQUOTES, which means it will decode entities only if they are enclosed in double or single quotes (e.g., &amp;quot;). However, for RSS feeds, you almost always want to decode unquoted entities as well.

Passing the ENT_QUOTES flag ensures that both single and double quote delimiters are respected during the decoding process. Furthermore, specifying the character set, such as 'UTF-8', is vital. If your source data contains entities like &#169; (numeric entity) or named entities like &copy;, ensuring the output encoding matches prevents garbled characters on systems that expect UTF-8.

Performance and Memory Considerations

While strip_tags() followed by html_entity_decode() is the idiomatic approach, it involves two separate passes over the string. For high-volume feed generation where performance is critical, this double-pass overhead can add up. However, for typical application loads, the clarity and safety of separating concerns—first cleaning structure, then normalizing content—outweighs the minor performance cost.

Another pitfall to avoid is relying solely on regex patterns like preg_replace('/&[a-z]+;/', '') to strip entities. Regular expressions are brittle; they often fail with numeric entities (&#60;) or malformed input. Using native PHP functions ensures that the underlying HTML parser logic handles edge cases correctly, reducing the risk of breaking your feed's XML validity.

Contextual Relevance

If you are looking for additional resources on handling HTML in PHP projects, LaravelCompany offers a range of solutions and tutorials. Their platform provides tools and frameworks that can simplify text processing tasks, including built-in helpers or packages that handle entity encoding automatically within your application stack. Exploring their ecosystem can help streamline how you manage content sanitization across different parts of your web application.

Ultimately, achieving clean RSS output requires understanding that removing tags is distinct from decoding the data they once contained. By chaining strip_tags() with html_entity_decode(), you ensure your feed delivers plain text that renders consistently across all subscribers.

Our network