Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

str_replace() leaves the non-breaking space (\u00a0) in the string

Errorstr_replace('\u00a0', '', $text) does not change the string

Short answer

In PHP, '\u00a0' is not a Unicode escape. In single quotes it is just those six characters, and without braces it is literal even in double quotes, so str_replace finds nothing. Use "\u{00A0}" in double quotes, or preg_replace with the u modifier. trim() will not remove it either.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Show the raw bytes of the character. If you see c2 a0, it is a UTF-8 non-breaking space; if you see the text  , it is still an HTML entity.

    php -r '$s = "a\u{00A0}b"; echo bin2hex($s), PHP_EOL;'
  2. 2Match what you see, then jump to its fix

    1. 1str_replace returns the string unchangedThe search string is a literal backslash-u text, not the characterFix
    2. 2str_replace("\xA0", ...) changes nothing, or breaks other UTF-8 lettersOnly one byte of the character was searched forFix
    3. 3the string contains the characters   or trim() cannot strip the decoded valueThe text still holds an   entityFix

What does the error look like?

  • str_replace('\u00a0', '', $text) does not change the string
  • trim() leaves a space at the end of the value

Cause 1 of 3

The search string is a literal backslash-u text, not the character

1Check

PHP has no \u00a0 escape. The manual defines \u{...} with braces, and says the braces are required. In single-quoted strings no escapes are processed at all.

var_dump('\u00a0' === "\u{00A0}");

2Fix

Write the character with the braced escape inside double quotes.

<?php
$clean = str_replace("\u{00A0}", ' ', $text);

3Verify

The check prints that the string no longer contains the character.

var_dump(str_contains($clean, "\u{00A0}"));

Confirmed by php.net

Did this fix it?

Cause 2 of 3

Only one byte of the character was searched for

1Check

\xA0 is a single byte. The braced escape yields the UTF-8 form of the code point, so a lone 0xA0 byte does not match a UTF-8 text.

echo bin2hex("\u{00A0}"), PHP_EOL;

2Fix

Match the code point as a whole with a regex. The u modifier makes pattern and subject UTF-8, and one call can also remove runs of them.

<?php
$clean = preg_replace('/\x{00A0}+/u', ' ', $text);

3Verify

Run it on the problem string and test that no code point U+00A0 is left.

var_dump(preg_match('/\x{00A0}/u', $clean));

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The text still holds an &nbsp; entity

1Check

The manual notes that html_entity_decode('&nbsp;') is not the ordinary space that trim() removes, so trim leaves it behind. If the value comes from HTML, decode it first, with the encoding given explicitly.

var_dump(trim(html_entity_decode('&nbsp;', ENT_QUOTES | ENT_HTML401, 'UTF-8')) === '');

2Fix

Decode, then replace the resulting character, as in the first fix.

<?php
$decoded = html_entity_decode($html, ENT_QUOTES | ENT_HTML401, 'UTF-8');
$clean   = preg_replace('/\x{00A0}+/u', ' ', $decoded);

3Verify

The cleaned value compares equal to the plain text you expect.

var_dump($clean === 'Total 10');

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Normalise user input once at the point it enters your code, not at every place it is used.
  • Keep your source files and the database in UTF-8, so a regex with the u modifier works.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.