How can I sort an array of UTF-8 strings in PHP? How can I sort an array of UTF-8 strings in PHP? arrays arrays

How can I sort an array of UTF-8 strings in PHP?


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);

Output:

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


I think you can use strcoll:

setlocale(LC_COLLATE, 'nl_BE.utf8');$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel');usort($array, 'strcoll'); print_r($array);

Result:

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

You need the nl_BE.utf8 locale on your system:

fy@Heisenberg:~$ locale -a | grep nl_BE.utf8nl_BE.utf8

If you are using debian you can use dpkg --reconfigure locales to add locales.


This script should resolve in a custom way. I hope it help. Note the mb_strtolower function. You need to use it do make the function case insensitive. The reason why I didn't use the strtolower function is that it does not work well with special chars.

<?phpfunction customSort($a, $b) {    static $charOrder = array('a', 'b', 'c', 'd', 'e', 'é',                              'f', 'g', 'h', 'i', 'j',                              'k', 'l', 'm', 'n', 'o',                              'p', 'q', 'r', 's', 't',                              'u', 'v', 'w', 'x', 'y', 'z');    $a = mb_strtolower($a);    $b = mb_strtolower($b);    for($i=0;$i<mb_strlen($a) && $i<mb_strlen($b);$i++) {        $chA = mb_substr($a, $i, 1);        $chB = mb_substr($b, $i, 1);        $valA = array_search($chA, $charOrder);        $valB = array_search($chB, $charOrder);        if($valA == $valB) continue;        if($valA > $valB) return 1;        return -1;    }    if(mb_strlen($a) == mb_strlen($b)) return 0;    if(mb_strlen($a) > mb_strlen($b))  return -1;    return 1;}$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel');usort($array, 'customSort');

EDIT: Sorry. I made many mistakes in the last code. Now is tested.

EDIT {2}: Everything with multibyte functions.