简体   繁体   中英

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM