开发者

locale aware string comparision

开发者 https://www.devze.com 2023-01-06 06:36 出处:网络
I´m using strcmp in combination with usort in order to sort an array of country names. Currently, the sort order is:

I´m using strcmp in combination with usort in order to sort an array of country names. Currently, the sort order is:

Belgien
Frankreich
Italien
Luxemburg
Niederlande
Spanien
United Kingdom
Österreich

Which is correct, apart from the position of Österreich. It sh开发者_运维问答ould be between Niederlande and Spanien.

I also tried strnatcmp and strcoll (with setlocale), but the sort order was not the way I wanted it. The results are not from a mysql db, so sorting via a mysql query is not an option.


Old question, meanwhile I am working at another company on another project, but faced recently the same problem. What finally worked was installing the intl extension for PHP.

sudo apt-get install php5-intl

And then using:

$arr = array(
"Belgien",
"Frankreich",
"Italien",
"Luxemburg",
"Niederlande",
"United Kingdom",
"Österreich",
"Spanien",
"Ásdf",
);

$coll = collator_create('de_DE');
$coll->sort($arr);
print_r($arr);

Returned the results in the expected order:

Array
(
    [0] => Ásdf
    [1] => Belgien
    [2] => Frankreich
    [3] => Italien
    [4] => Luxemburg
    [5] => Niederlande
    [6] => Österreich
    [7] => Spanien
    [8] => United Kingdom
)


This works (assumes script is in UTF-8):

<?php

$arr = array(
"Belgien",
"Frankreich",
"Italien",
"Luxemburg",
"Niederlande",
"United Kingdom",
"Österreich",
"Spanien",
"Ásdf",
);

setlocale(LC_COLLATE, "pt_PT.UTF8");
usort($arr, 'strcoll');
print_r($arr);

gives me:

Array
(
    [0] => Ásdf
    [1] => Belgien
    [2] => Frankreich
    [3] => Italien
    [4] => Luxemburg
    [5] => Niederlande
    [6] => Österreich
    [7] => Spanien
    [8] => United Kingdom
)

However, this is painful; it requires the locale to be installed. locale -a gives you the installed locales, e.g. in my machine it gives me:

C
en_US
en_US.iso88591
en_US.utf8
POSIX
pt_PT.utf8
0

精彩评论

暂无评论...
验证码 换一张
取 消