String comparison with diacritics not working in PHP?
Hello everyone, I've been pondering about why the string comparison (with diacritics, a.k.a. special characters like ěščřžý) does not work properly in PHP on MacOS. I'm also working with Rocky Linux and Windows and on both the string comparison in PHP works as expected.
I'm reading the string (with diacritics) from MySQL database or from a file path. Then I try comparing it with strpos(), strcmp() and also with mb_strpos() and mb_strcmp() and none of them works.
Curiously, I've found 2 workarounds:
1) Compare with regular expression (preg_match()) and substitue the special character with ".*" or ".{2}", eg. instead of "SUŠG" I search for "SU.*G" - this works. Interestingly enough, with "SU.{1}G" it DOESN'T work. But when I use "SU.{2}G", it works again. Therefore I'm thinking that the whole issue comes from system locale or something similar, since regex thinks, the "Š" is actually two characters instead of one.
2) Before comparison, use the Normalizer::normalize() function. After putting my DB result (or file path) inside the normalize() function, all of the comparison functions work as expected.
However, even if I've found these two workarounds, none of them is the preferred way, since regexes shouldn't be used when there is easier (eg. more efficient) way and also, treating every involved line of code with normalize() just because of one OS, is insane and unacceptable - since it bloats the code and makes maintenance harder.
Can anyone, please, point me in the right direction? I've tried changing the locale with PHP setlocale( LC_ALL, 'cs_CZ' ), tried changing it also with terminal, and tried changing the whole system language from English to Czech but nothing works.
I've also found that C# code with diacritics (in comments and messages) that I write on Windows (with Visual Studio) shows on my Mac Visual Studio as "?" instead of the original character. I think both of these issues are related.
Do you know what is the root of this and how to fix it?
MacBook Pro 16″, macOS 13.4