如何在 PHP 中对一组 UTF-8 字符串进行排序? [英] How can I sort an array of UTF-8 strings in PHP?

查看:23
本文介绍了如何在 PHP 中对一组 UTF-8 字符串进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助按 utf-8 对单词进行排序.例如,我们有 5 个来自比利时的城市.

need help with sorting words by utf-8. For example, we have 5 cities from Belgium.

$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel');
sort($array); // Expected: Aubel, Borgloon, Éghezée, Lennik, Thuin
              // Actual: Aubel, Borgloon, Lennik, Thuin, Éghezée

城市 Éghezée 应该是第三.是否可以使用/设置某种 utf-8 或创建我自己的字符顺序?

City Éghezée should be third. Is it possible to use/set some kind of utf-8 or create my own character order?

推荐答案

intl 捆绑提供使用 PHP 5.3 中的 PHP 和 它仅支持 UTF-8.

intl comes bundled with PHP from PHP 5.3 and it only supports UTF-8.

在这种情况下,您可以使用 Collat​​or:

You can use a Collator in this case:

$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel');
$collator = new Collator('en_US');
$collator->sort($array);
print_r($array);

输出:

Array
(
    [0] => Aubel
    [1] => Borgloon
    [2] => Éghezée
    [3] => Lennik
    [4] => Thuin
)

这篇关于如何在 PHP 中对一组 UTF-8 字符串进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆