locale aware string comparision

笑着哭i 提交于 2019-12-22 15:38:14

问题


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 should 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.


回答1:


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
)



回答2:


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


来源:https://stackoverflow.com/questions/3204136/locale-aware-string-comparision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!