str_replace() leaves the non-breaking space (\u00a0) in the string
str_replace('\u00a0', '', $text) does not change the stringShort 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.
Fix it fast
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;'2Match what you see, then jump to its fix
- 1
str_replace returns the string unchangedThe search string is a literal backslash-u text, not the characterFix - 2
str_replace("\xA0", ...) changes nothing, or breaks other UTF-8 lettersOnly one byte of the character was searched forFix - 3
the string contains the characters or trim() cannot strip the decoded valueThe text still holds an entityFix
- 1
What does the error look like?
str_replace('\u00a0', '', $text) does not change the stringtrim() 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
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
Cause 3 of 3
The text still holds an entity
1Check
The manual notes that html_entity_decode(' ') 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(' ', 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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix