Decoding with html_entity_decode, and then having problem with
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 开发者_如何学Pythona 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] == ' ')
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 bytrim()
) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset.
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...)
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...
精彩评论