0

所以我有一个带有域名的数组

Array () {
    [0] => Array () {
        [id]=>int
        [name]=>string
        [something]=>string/int...
    }
    [38] => Array () {

    }
}

我需要在表中显示域名,我从 mysql 表中按名称 ASC 对它们进行排序,循环遍历数组,然后转换 $domain['name'] 以显示不像 xn--sfasdf-dfg 的 IDN .com,但人类可读。但是这个域名介于 W 和 Y 之间,因为它们以 X 开头,我想要的是按照它们的 unicode 名称对它们进行排序 някво-име.com (它以 N 开头)这个域名应该在 M 和 O 之间,而不是在W 和 Y。有人知道如何做到这一点吗?我在谷歌上搜索过,但似乎没有人写过这样的问题。我尝试了 usort 和 uasort php 函数,但顺序完全错误。

谢谢, 亚尼克里斯特夫

4

2 回答 2

0

Sorting (or collating for the technical term) is a complicated matter, already when inside one language but even more so if you mix languages. There is no context associated with a domain name string, you do not know in which language it is, you just have a string with characters (even between all languages using only ASCII the sorting order is not the same).

So you will first need to sort this out and define in which language you want to operate. Then you have tools to do the collating, but they are not basic tool.

For example, in libc the LC_COLLATE variable will influence all tools correctly programmed for internationalisation, and thus taking it into account.

libicu is another big library handling things like that, see https://www-01.ibm.com/software/globalization/icu/

PHP has the intl extension that is a wrapper around it. See specifically the Collator class at http://php.net/manual/en/class.collator.php :

Provides string comparison capability with support for appropriate locale-sensitive sort orderings.

于 2018-02-10T04:24:57.993 回答
-1

някво-име.com不以“N”开头,它以西里尔字母“AN”开头,听起来像英语“N”。如果你想根据字母的发音进行排序,你会遇到困难的时候。如果你想按字母排序,它是一个不同的字母,应该在 Z 之后或 A 之前。

我会将所有域解码为 un​​icode,也将英文域转换为 unicode,然后使用任何用于 unicode 排序的普通内置函数对 unicode 列表进行排序。

于 2013-10-20T10:43:24.497 回答