我怎么能进行排序PHP中的UTF-8字符串? [英] How can I sort a UTF-8 string in PHP?
本文介绍了我怎么能进行排序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
)
这篇关于我怎么能进行排序PHP中的UTF-8字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文