I'm trying to sort an array of French words alphabetically. Here's what I did:
$locale = setlocale(LC_ALL, 'fr_FR.UTF-8');
print_r($locale);
echo PHP_EOL;
$verbs = [
'être',
'faire',
'ecouter',
'dire',
'euphoriser',
];
sort($verbs, SORT_LOCALE_STRING);
print_r($verbs);
On my web server, it works as expected, printing:
fr_FR.UTF-8
Array
(
[0] => dire
[1] => ecouter
[2] => être
[3] => euphoriser
[4] => faire
)
The accent is ignored and ê
behaves like e
. But there's a problem with PHP in my console on macOS. The same app prints the following result:
fr_FR.UTF-8
Array
(
[0] => dire
[1] => ecouter
[2] => euphoriser
[3] => faire
[4] => être
)
The locale is found, because setlocale()
returns fr_FR.UTF-8
correctly (it returns false
for invalid locale). But for some reason, être
is the last element of the array after sorting. It's like this French locale is broken, or never used. How can I make my console PHP use locale correctly?
When I run locale -a
, fr_FR.UTF-8
is listed as one of available locales on my machine.
I'm using PHP 7.4.9.