tags:

views:

154

answers:

3

After decoding the special character   with the html_entity_decode function, I get spaces in replace of   entities. My problem is when I check if if ($decoded_str[5] == ' ') it isn't true, though in $decoded_str[5] there is a space that was   before decoding. How to settle the matter? I need to be able to check it like this way: if ($decoded_str[5] == ' ')

+2  A: 

nbsp has a character code of 0xA0, and the space is 0x20.

Depending on your encoding, you may need to compare (ISO-8859-1 / default)

if ($decoded_str[5] === '\xa0')

or (UTF-8)

if ($decoded_str[5] === '\xc2' && $decoded_str[6] === '\xa0')

From the manual of html_entity_decode:

Note: You might wonder why trim(html_entity_decode(' ')); doesn't reduce the string to an empty string, that's because the ' ' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset.

KennyTM
+1  A: 

This is because   is not a space : it's a Non-breaking space.

This means its character code is not 0x20, but 0xA0 (well, of course, this depends on the charset, I suppose...)

Pascal MARTIN
0x0a is an `\n`....
KennyTM
@Kenny > ergh ; one typo in a post, and it's just where it must not be ;-( thanks for the comment : I've corrected that :-)
Pascal MARTIN
A: 

it's the right behaviour, because html_entity_decode converts all applicable html characters, so also & so you could check the space using this if..

if (htmlentities($dec) == ' ')

which is basically the comparison with the original value of the string...

Marcx
no the decode functon doens't return a space...
Marcx