Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to sort an array of UTF-8 strings?
    text
    copied!<p>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 problem) is no solution. The following does not work on my windows development machine (although I'd think that this should be at least a possible solution):</p> <pre><code>$array=array('Birnen', 'Äpfel', 'Ungetüme', 'Apfel', 'Ungetiere', 'Österreich'); $oldLocal=setlocale(LC_COLLATE, "0"); var_dump(setlocale(LC_COLLATE, 'German_Germany.65001')); usort($array, 'strcoll'); var_dump(setlocale(LC_COLLATE, $oldLocal)); var_dump($array); </code></pre> <p>The output is:</p> <pre><code>string(20) "German_Germany.65001" string(1) "C" array(6) { [0]=&gt; string(6) "Birnen" [1]=&gt; string(9) "Ungetiere" [2]=&gt; string(6) "Äpfel" [3]=&gt; string(5) "Apfel" [4]=&gt; string(9) "Ungetüme" [5]=&gt; string(11) "Österreich" } </code></pre> <p>This is complete nonsense. Using 1252 as the codepage for <code>setlocale()</code> gives another output but still a plainly wrong one:</p> <pre><code>string(19) "German_Germany.1252" string(1) "C" array(6) { [0]=&gt; string(11) "Österreich" [1]=&gt; string(6) "Äpfel" [2]=&gt; string(5) "Apfel" [3]=&gt; string(6) "Birnen" [4]=&gt; string(9) "Ungetüme" [5]=&gt; string(9) "Ungetiere" } </code></pre> <p>Is there a way to sort an array with UTF-8 strings locale aware?</p> <p><em>Just noted that this seems to be PHP on Windows problem, as the same snippet with <code>de_DE.utf8</code> used as locale works on a Linux machine. Nevertheless a solution for this Windows-specific problem would be nice...</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload