I have a string with "\u00a0", and I need to replace it with "" str_replace fails

I have a string with "\u00a0", and I need to replace it with "" str_replace fails

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

Stefan Bogdanescu

Founder & Senior Architect

The \u00a0 Trap: Why str_replace Fails on Non-Breaking Spaces

When cleaning strings imported from Microsoft Office suites like Excel, Access, or Word, developers often encounter invisible characters that break parsing logic. You might successfully strip carriage returns (\r) and newlines (\n) using standard string functions, but the non-breaking space (\u00a0) behaves differently in PHP's str_replace. This behavior isn't a bug; it is a fundamental distinction between how strings are represented in memory versus how they are displayed.

The core issue lies in understanding that \u00a0 represents the Unicode character U+00A0 (the Non-Breaking Space), not the literal four-character sequence backslash, u, zero, zero, a, zero. In your example, $string contains actual bytes corresponding to this character. When you call str_replace("\u00a0", "", $string), PHP interprets the first argument as a string containing those six specific characters: \, u, 0, 0, a, 0. Since your source string contains the single-byte non-breaking space, PHP cannot find a match for the literal escape sequence inside the string.

To fix this, you must target the actual byte value of the character. The most reliable method is to use the hex representation of the Unicode code point. You can achieve this using chr() combined with hexadecimal conversion:

$nonBreakingSpace = chr(hexdec('00A0')); // Returns the actual non-breaking space character

$clean = str_replace($nonBreakingSpace, '', $string);

Alternatively, if you prefer decimal notation, you can use chr(160) directly. This approach ensures that str_replace is comparing the actual data present in the string against the actual target byte, rather than a textual representation of an escape sequence.

Beyond Simple Replacement: The Case for Normalization

While str_replace works perfectly once the correct byte sequence is identified, relying solely on character-by-character replacement can become unmanageable when dealing with complex strings from mixed-encoding sources. Microsoft Office applications often introduce a variety of whitespace characters: standard spaces (U+0020), tabs (U+0009), non-breaking spaces (U+00A0), and various zero-width joiners or non-joiners.

For production environments where robustness is critical, it is often better to normalize the string first. The preg_replace function allows you to define a pattern that matches any Unicode whitespace character and replaces it with an empty string or a standard space:

// Matches any Unicode whitespace category (includes Nbsp, Tab, etc.)
$clean = preg_replace('/\p{Z}++/', ' ', $string);

This regular expression uses the \p{Z} property to match any separator character defined in the Unicode standard. The + quantifier ensures it handles consecutive whitespace sequences as a single unit, preventing multiple spaces from collapsing incorrectly depending on your desired output format.

The Myth of "Stripping Every Possible Character"

The original query mentions a desire for a function that strips "every possible char of every possible encoding." It is important to clarify that no such universal function exists because text processing requires context. If you strip all non-printable characters indiscriminately, you risk destroying valid data, such as HTML entities, JSON escape sequences, or legitimate control characters like newlines in a multi-line string.

If your goal is to sanitize input for storage in a database or display on a web page, the strategy should be specific:

  1. Normalization: Replace all variant whitespace (tabs, non-breaking spaces) with standard spaces using preg_replace or str_ireplace.
  2. Trimming: Use trim() to remove leading and trailing whitespace from the boundaries of the string.
  3. Contextual Sanitization: If the goal is to extract email addresses, focus on the logic that parses the domain rather than blindly cleaning the entire buffer.

For projects using Laravel, you might find utility in packages that handle text normalization, but native PHP functions remain the most performant choice for high-volume string manipulation. Always remember that json_encode outputs escape sequences like \u00a0 only when debugging or converting data to JSON; the underlying string variable holds the raw bytes. Your cleaning logic must operate on those raw bytes, not the escaped representation shown in a debugger.

Our network