+ 4
[SOLVED] PHP strcoll not working for Swedish language.
I am having trouble sorting non-latin alphabets alphabetically in PHP. The server does not have intl extension so I am stuck with strcoll. Does anyone have an idea what's wrong? https://code.sololearn.com/wA9A3A1764a2/
9 Respuestas
+ 6
Ore Based on what I've been reading, this can be an issue if you don't have control over the server.
One option you might be able to do is to roll your own callback function that converts the unicode mbstring characters into ASCII equivalents and them do the comparisons using the the ASCII text.
Here are a couple of links you can use to piece a solution together such that "Cliënt" is converted to "Client":
https://sodocumentation.net/php/topic/4472/unicode-support-in-php
Or... you can roll your own translit mapping of special characters to ASCII in a custom sorting callback function as shown here:
https://stackoverflow.com/questions/43614616/strcoll-utf-8-sorting-php
The answer in this question does a great job explaining the issue with unicode sorting.
While this link is a bit dated, it gives some insight on the issue in PHP:
https://softwareengineering.stackexchange.com/questions/49624/what-does-the-lack-of-unicode-support-in-php-mean
+ 5
Ore Based on the link below:
https://www.php.net/manual/en/function.strcoll.php
... it's possible that the locale is, in fact, only C or POSIX on the SL environment and the setlocale may not actually be changing anything.
"strcoll() uses the current locale for doing the comparisons. If the current locale is C or POSIX, this function is equivalent to strcmp()."
According to the link below:
https://www.php.net/manual/en/function.setlocale.php
The locale is set per process rather than thread... which seems odd as the locale could be changed by another script on another thread.
I wonder why this wasn't set per thread. 🤷♂️
+ 4
Kode Krasher and Rei The sort order issues may be more obvious when running this version below and scrolling to the very bottom.
https://code.sololearn.com/wC9302uZTWOD/?ref=app
Ore The issue with the strcoll($a, $b) function seems to be that it behaves no differently than strcmp($a, $b) and the locale doesn't seem to actually make a difference in the SL environment. It doesn't behave differently for me on my local dev environment either.
I'm not sure why. But it seems to be environment specific.
----
Alternatively, as you may already know, Collator::compare($a, $b) seems to be more reliable based on tests in my local development environment.
I've posted an alternative code below using this solution:
https://code.sololearn.com/wWT2J82hz0vV/?ref=app
NOTE for those unaware: This code will only work if the php_intl extension is enabled in the php.ini, which appears not to be the case on SoloLearn.
Additional details can be viewed when running this 2nd code.
+ 4
Ore Unfortunately, I'm not aware of any other options for working around this issue.
TBH... I'm quite surprised that the strcoll function may be so problematic as indicated in the top 2 comments from the link below:
https://www.php.net/manual/en/function.strcoll.php
+ 3
@David Carroll Thanks for the answer. As I said earlier "The server does not have intl extension so I am stuck with strcoll." Do you know of any alternative to strcoll that works without intl?
+ 3
David Carroll Okay. Thanks for the links. I will see what I can do.
EDIT: It works now after following that SO link
https://stackoverflow.com/questions/43614616/strcoll-utf-8-sorting-php
https://code.sololearn.com/wA34A11A17A2
0
0
i'm in android, its sorted here. hmm