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
)

Leave a Comment