Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with string comparison in PHP

I have two strings with seemingly the same values. One is stored as a key in an array, the other a value in another different array. I compare the two using ==, ===, and strcmp. All treat them as different strings. I do a var_dump and this is what I get.

string(17) "Valentine’s Day" 
string(15) "Valentine's Day"

Does anyone have any idea why the first string would be 17 characters and the second 15?

Update: This is slightly more obvious when I pasted this out of my editor whose font made the two different apostrophe's almost indistinguishable.

like image 785
Chestone Avatar asked Jan 21 '23 19:01

Chestone


2 Answers

The first string contains a Unicode character for the apostrophe while the second string just has a regular ASCII ' character.

The Unicode character takes up more space.

If you run the PHP ord() function on each of those characters you'll see that you get different values for each:

echo ord("’"); //226 This is just the first 2 bytes (see comments below for details from ircmaxell)
echo ord("'"); //27
like image 58
Mark Biek Avatar answered Jan 23 '23 10:01

Mark Biek


As a complement to @Mark answer above which is right (the is a multi-byte character, most probably UTF-8, while ' is not). You can easily convert it to ASCII (or ISO-8859-1) using iconv, per example:

echo iconv('utf-8', 'ascii//TRANSLIT', $str);

Note: Not all characters can be transformed from multi-byte to ASCII or latin1. You can use //IGNORE to have them removed from the resulting string.

like image 24
netcoder Avatar answered Jan 23 '23 08:01

netcoder