区域设置感知字符串比较

发布于 2024-09-08 08:12:20 字数 438 浏览 1 评论 0原文

我将 strcmpusort 结合使用,以便对国家/地区名称数组进行排序。目前,排序顺序是:

Belgien
Frankreich
Italien
Luxemburg
Niederlande
Spanien
United Kingdom
Österreich

除了 Österreich 的位置之外,这是正确的。它应该位于 NiederlandeSpanien 之间。

我还尝试了 strnatcmpstrcoll (使用 setlocale),但排序顺序不是我想要的方式。结果不是来自 mysql 数据库,因此不能选择通过 mysql 查询进行排序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-09-15 08:12:20

老问题,同时我正在另一家公司从事另一个项目,但最近遇到了同样的问题。最终起作用的是安装 PHP 的 intl 扩展

sudo apt-get install php5-intl

然后使用:

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

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

按预期顺序返回结果:

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

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
)
木緿 2024-09-15 08:12:20

这有效(假设脚本采用 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);

给我:

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

但是,这很痛苦;它需要安装区域设置。 locale -a 为您提供已安装的区域设置,例如在我的机器中它为我提供:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文