1

I'm trying to compare 2 different special characters, but they are equal when I compare them.

$char1= "";
$char2= "";

echo mb_detect_encoding($char1, 'UTF-8', true); // 'UTF-8'
echo mb_detect_encoding($char2, 'UTF-8', true); // 'UTF-8'

if($char1 == $char2) // TRUE
echo strcmp($char1, $char2); // 0

Apache/2.4.10

PHP/5.6.3

PhpStorm 9.0.2

What can I do to make a valid comparison?

Thank you.

Boy
  • 1,182
  • 2
  • 11
  • 28

1 Answers1

2

What can I do to make a valid comparison?

Updated Solution

Perhaps we're looking at this the wrong way. The problem may have nothing to do with PHP, but be about your code editor instead. Perhaps the editor is registering both chars as the same when you enter them, so PHP doesn't see any difference. Here is what you can do:

  1. Save each char in a file by itself, using an editor that recognizes the character, such as Wordpad
  2. Load the character in PHP with $char=file_get_contents('path/to/char.txt')
  3. Now that we've bypassed your code editor entirely, compare the two. If they're different, your editor might be to blame.

Original Solution

You could try to convert your characters to their ASCII values and compare the values instead of the characters

$ordUTF8 = function($char){
    list(, $ord) = unpack('N', mb_convert_encoding($char, 'UCS-4BE', 'UTF-8'));
    return $ord;
};
$char1= "";
$char2= "";
// 61656 and 61558 in my testing
$isEqual = $ordUTF8($char1)===$ordUTF8($char2);

Live demo. This solution was inspired by this accepted answer

Community
  • 1
  • 1
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • Returns true... :( – Boy Jul 17 '16 at 06:59
  • 1
    Perhaps we're looking at it from the wrong angle. Maybe it has to do with your code editor? What if you save each character as its own text file, using an editor like Wordpad. Then in PHP, use `$char = file_get_contents('path/to/file.txt')` to retrieve the character? – BeetleJuice Jul 17 '16 at 07:07
  • I think its some apache configuration problem... I tried setting default charset, tried to read from another file, still incorrect :/ – Boy Jul 17 '16 at 07:08
  • You were right, I put each char as separate file, and it works now, lol – Boy Jul 17 '16 at 07:16