我怎么能进行排序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
市埃格泽应该是第三个。是否有可能使用/设置某种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 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
)