How to sort an array of UTF-8 strings?

后端 未结 8 768
刺人心
刺人心 2020-11-27 04:32

I currentyl have no clue on how to sort an array which contains UTF-8 encoded strings in PHP. The array comes from a LDAP server so sorting via a database (would be no probl

相关标签:
8条回答
  • 2020-11-27 05:04

    Update on this issue:

    Even though the discussion around this problem revealed that we could have discovered a PHP bug with strcoll() and/or setlocale(), this is clearly not the case. The problem is rather a limitation of the Windows CRT implementation of setlocale() (PHPs setlocale() is just a thin wrapper around the CRT call). The following is a citation of the MSDN page "setlocale, _wsetlocale":

    The set of available languages, country/region codes, and code pages includes all those supported by the Win32 NLS API except code pages that require more than two bytes per character, such as UTF-7 and UTF-8. If you provide a code page like UTF-7 or UTF-8, setlocale will fail, returning NULL. The set of language and country/region codes supported by setlocale is listed in Language and Country/Region Strings.

    It therefore is impossible to use locale-aware string operations within PHP on Windows when strings are multi-byte encoded.

    0 讨论(0)
  • 2020-11-27 05:17

    I am confronted with the same problem with German "Umlaute". After some research, this worked for me:

    $laender =array("Österreich", "Schweiz", "England", "France", "Ägypten");  
    $laender = array_map("utf8_decode", $laender);  
    setlocale(LC_ALL,"de_DE@euro", "de_DE", "deu_deu");  
    sort($laender, SORT_LOCALE_STRING);  
    $laender = array_map("utf8_encode", $laender);  
    print_r($laender);
    

    The result:

    Array
    (
    [0] => Ägypten
    [1] => England
    [2] => France
    [3] => Österreich
    [4] => Schweiz
    )

    0 讨论(0)
提交回复
热议问题