replace   characters that are hidden in text

replace   characters that are hidden in text

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

Stefan Bogdanescu

Founder & Senior Architect

Cleaning Up Invisible Text: Removing   Without Breaking Layouts

You've encountered a classic web development headache: text that looks perfectly fine in a browser but causes layout shifts or rendering issues when processed server-side. The culprit is often the non-breaking space entity,  . These characters are invisible to the naked eye but occupy horizontal space and prevent word wrapping. If you're trying to normalize data from a CMS, scrape content from a legacy site, or sanitize user input, simply stripping them out can break your design if you aren't careful about what else you remove.

The immediate instinct is often to use trim() or a simple string replacement. However, both approaches fail here for specific reasons that are easy to miss. The trim() function only removes whitespace from the start and end of a string; it cannot touch spaces embedded within the text body. Furthermore, standard string replacements like str_replace(' ', '', $string) often fail because the HTML parser has already converted those entities into actual Unicode space characters (U+00A0). By the time your code sees the string, it usually contains the raw invisible character, not the literal ampersand sequence.

To handle this correctly, you must distinguish between the literal HTML entity and the rendered character. If your input is a raw HTML string where the entity   still exists as text, a simple replacement works. However, if the browser or an upstream parser has already rendered it, you are dealing with a Unicode Non-Breaking Space.

A robust solution involves using regular expressions that target both scenarios. You need to match the literal entity sequence and the actual Unicode character. In PHP, this looks like:

$cleaned = preg_replace('/ |[^\x00-\x7F\s]/u', '', $string);

Wait, that regex is too aggressive if you want to keep other special characters. Let's refine it. We need to target the specific non-breaking space character (hex A0) and the literal entity string, while preserving everything else including <br>, Unicode letters like Persian or Arabic digits, and standard whitespace.

// Target both the literal entity and the actual Unicode NBSP character
$cleaned = preg_replace('/&nbsp;|\xC2\xA0/', '', $string);

Here, \xC2\xA0 is the UTF-8 byte sequence for the non-breaking space. Using the u modifier ensures PHP treats the string as UTF-8 and handles multi-byte characters correctly. This approach leaves <br> tags intact and preserves valid Unicode ranges, which is critical when dealing with internationalized content.

There is a significant trade-off to consider here: whitespace normalization. When you remove all instances of non-breaking spaces, you might inadvertently merge words that were intended to stay separate or alter the spacing around punctuation. If your goal is purely visual cleanup before storage, this is fine. However, if you are preparing text for display, you must ensure you don't collapse tabs or multiple standard spaces into a single gap unless that is explicitly desired.

Another common pitfall arises when mixing trim() with regex cleaning. Since trim() does not handle internal spaces, placing it after the regex replacement is redundant and adds unnecessary overhead. You should only trim if you specifically need to remove whitespace from the boundaries of the string, which is a separate concern from handling non-breaking entities.

If you are working within a Laravel application, the logic remains the same, but you can encapsulate this in a helper service or use Laravel's built-in string helpers if they meet your needs. However, for complex entity handling, writing a dedicated method ensures consistency across your codebase:

public function cleanNonBreakingSpaces($string)
{
    return preg_replace('/&nbsp;|\xC2\xA0/', '', $string);
}

This pattern is language-agnostic in concept. In JavaScript, you would use a similar regex with the u flag to match both the entity and the Unicode code point \xA0. The key takeaway is that ignoring the difference between raw HTML entities and rendered Unicode characters is the primary source of bugs in this domain. By explicitly targeting both forms, you ensure that your text processing pipeline respects the invisible characters without letting them dictate your layout logic.

Our network