5

I have the following string:

Hello.   Hello.

If you look at the string in a hex editor it looks like this:

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

Note the A0 in the middle. (This is the no-break space character).

A0 is breaking some JavaScript I am using so I would like to remove it when the string is being pre-processed by a PHP script.

If I use the following code:

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

the A0 gets replaced with 00 which is also a troublesome character.
As you can see from the preg_replace function, it should be replace by a space, or 20.

Do any of you know how I can get rid of this troublesome A0 character?

Thank you.

EDIT: I am using Windows-1252 and cannot switch to UTF-8. This won't be a problem if you are using UTF-8...

4

2 回答 2

3

简单的

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

简单测试

$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));

输出

string 'Hello.    Hello.' (length=16)
string '48656c6c6f2e2020202048656c6c6f2e' (length=32) 
                     ^---------------------- 0A Replaced with 02   
于 2012-11-04T18:57:34.033 回答
2

我想出了一个解决方案:

首先转换编码类型,然后进行替换:

$text = mb_convert_encoding($text, "Windows-1252", "UTF-8");
$text = preg_replace("/\xA0/"," ", $text);
于 2012-11-04T19:08:15.507 回答