Does anyone know how I can get rid of hex A0 from a string?

I have the following line:

Hello. Hello.

If you look at a line in a hex editor, it looks like this:

48 65 6C 6C 6F 2E 20 A0 20 20 48 65 6C 6C 6F 2E

Pay attention to A0 in the middle. (This is a space character without a space).

A0 breaks the JavaScript that I use, so I would like to remove it when the string is pre-processed by a PHP script.

If I use the following code:

 $text = preg_replace("/\xA0/"," ", $text); 

A0 is replaced by 00 , which is also an unpleasant symbol.
As you can see from the preg_replace function, it should be replaced with a space or 20 .

Do any of you know how I can get rid of this unpleasant A0 symbol?

Thanks.

EDIT: I am using Windows-1252 and cannot switch to UTF-8. This will not be a problem if you use UTF-8 ...

+4
source share
2 answers

I understood the solution:

Convert the encoding type first, and then do the replacement:

 $text = mb_convert_encoding($text, "Windows-1252", "UTF-8"); $text = preg_replace("/\xA0/"," ", $text); 
+2
source

Plain

 $string = str_replace(chr(160), " ", $string); 

Simple test

 $string = "48656C6C6F2E20A0202048656C6C6F2E" ; ^----------------------- 0A //Rebuild String $string = pack("H*",$string); //Replace 0A Charater $string = str_replace(chr(160), " ", $string); //Send Output var_dump($string,bin2hex($string)); 

Output

 string 'Hello. Hello.' (length=16) string '48656c6c6f2e2020202048656c6c6f2e' (length=32) ^---------------------- 0A Replaced with 02 
+3
source

Source: https://habr.com/ru/post/1443983/


All Articles