Voting

: nine minus seven?
(Example: nine)

The Note You're Voting On

cmarshall at gmx dot de
13 years ago
I read up on various problems re: sort() and German Umlaut chars and my head was soon spinning - bug in sort() or not, solution via locale or not, etc. ... (a total newbie here).

The obvious solution for me was quick and dirty: transform the Umlaut chars (present as HTML codes in my case) to their normal equivalent ('ä' = 'ae', 'ö' = 'oe', 'ü' = 'ue', 'ß' = 'ss' etc.), sort the array, then transform back. However there are cases in which a 'Mueller' is really that and does NOT need to be transformed into 'Müller' afterwards. Hence I for example replace the Umlaut itself with it's normal equivalent plus a char not used in the string otherwise (e.g. '_') so that the transfer back to Umlaut would only take place on certain combinations.

Of course any other char instead of '_' can be used as additional char (influencing the sort result). I know that my solution is rough at the edges and may cause other sort problems but it was sufficient for my purpose.

The array '$dat' in this example was filled with German town names (I actually worked with a multiple array ('$dat[][]') but stripped the code down to this as it's easier to understand):

<?php
// START Pre-sorting (Umlaut -> normal letters)
$max = count($dat);
for(
$totcnt = 0; $totcnt < $max; $totcnt++){
$dat[$totcnt]=str_replace('&szlig;','ss_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('&Auml;','Ae_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('&auml;','ae_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('&Ouml;','Oe_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('&ouml;','oe_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('&Uuml;','Ue_',$dat[$totcnt]);
$dat[$totcnt]=str_replace('&uuml;','ue_',$dat[$totcnt]);
}
// END Pre-sorting (Umlaut -> normal letters)

// START Sorting //
function compare_towns($a, $b)
{
return
strnatcmp($a, $b);
}
usort($dat, 'compare_towns');
// END Sorting //

// START Post-sorting (normal letters -> Umlaut)
for($totcnt = 0; $totcnt < $max; $totcnt++){
$dat[$totcnt]=str_replace('ss_','&szlig;',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Ae_','&Auml;',$dat[$totcnt]);
$dat[$totcnt]=str_replace('ae_','&auml;',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Oe_','&Ouml;',$dat[$totcnt]);
$dat[$totcnt]=str_replace('oe_','&ouml;',$dat[$totcnt]);
$dat[$totcnt]=str_replace('Ue_','&Uuml;',$dat[$totcnt]);
$dat[$totcnt]=str_replace('ue_','&uuml;',$dat[$totcnt]);
}
// END Post-sorting (normal letters -> Umlaut)
?>

<< Back to user notes page

To Top