Anon21
Anon21

Reputation: 3073

Comparing utf8 encoded string in php

I am trying to compare a value submitted as a POST to an internal string. They are both utf8 encoded. This is the code

echo $_POST["province"] . "\n";
setlocale(LC_COLLATE, "fr_CA");
echo strcoll($_POST["province"], "Québec");

This code echos the following:

Québec
-38

strcoll should return 0 if the string match, not -38. In other words, the comparison fails. How do I compare two utf8 string that are identical to a human reader, but might be encoded differently? I have tried Normalizer:normalize, the common "==" operator, looked at multibyte php extension (but there seem to be no compare functions??) and nothing worked thus far for me.

Upvotes: 0

Views: 2874

Answers (1)

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

  1. Make sure that you save your PHP files as UTF-8, it really matters.
  2. Make sure that your HTML form actually sends UTF-8 data to the server.
  3. If your PHP doesn't use UTF-8 as the internal encoding, you may have to use utf8_decode to ensure that the strings you're checking are in the same encoding.

Upvotes: 5

Related Questions