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

查看:70
本文介绍了如何在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.

在这种情况下,您可以使用整理器:

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天全站免登陆