note 50607 added to function.usort
| From: | ulrichUNDERSCOREalexisatyahoodotcom at osu1 dot php dot net | Date: | Fri, 04 Mar 2005 15:03:10 +0000 |
| Subject: | note 50607 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85923@lists.php.net to get a copy of this message | ||
Please note that:
- the HTML entities should be replaced by their accented counterparts;
- the $patterns arrays have been split for display convenience.
<?php
/* returns the collated string (with no accent or with the matching string
* replacement) in lower case according to that language rule.
* The aim is to alphabetically sort lists of strings with characters from
* other languages but using collation rules of that given language
* (ISO 639-1 code).
* Latin-derived alphabets are supported (currently English, French and
* German)
* rules source: http://en.wikipedia.org/wiki/Latin_alphabet
*/
function collatedLower($string, $lang=null) {
switch ($lang) {
case 'de':
// German
$patterns = array(
"/á|à|â|ã|å|
Á|À|Â|Ã|Å/",
"/ä|æ|Ä|Æ/",
"/ç|Ç/",
"/é|è|ê|ë|
É|È|Ê|Ë/",
"/ì|í|î|ï|
Ì|Í|Î|Ï/",
"/ñ|Ñ/",
"/ò|ó|ô|õ|ø|
Ò|Ó|Ô|Õ|Ø/",
"/ö|œ|Ö|Œ/",
"/ß/",
"/ù|ú|û|
Ù|Ú|Û/",
"/ü|Ü/",
"/ý|ÿ|Ý/");
$replace = array('a', 'ae', 'c', 'e',
'i', 'n', 'o', 'oe', 'ss', 'u',
'ue', 'y');
break;
default:
// 'en' (English), 'fr' (French)
$patterns = array(
"/á|à|â|ã|ä|å|
Á|À|Â|Ã|Ä|Å/",
"/æ|Æ/",
"/ç|Ç/",
"/é|è|ê|ë|
É|È|Ê|Ë/",
"/ì|í|î|ï|
Ì|Í|Î|Ï/",
"/ñ|Ñ/",
"/ò|ó|ô|õ|ö|ø|
Ò|Ó|Ô|Õ|Ö|Ø/",
"/œ|Œ/",
"/ß/",
"/ù|ú|û|ü|
Ù|Ú|Û|Ü/",
"/ý|ÿ|Ý/");
$replace = array('a', 'ae', 'c', 'e',
'i', 'n', 'o', 'oe', 'ss', 'u',
'y');
break;
}
return preg_replace($patterns,$replace,$string);
}
// English
function compareAccents_en($a, $b) {
return compareAccents($a, $b, 'en');
}
// French
function compareAccents_fr($a, $b) {
return compareAccents($a, $b, 'fr');
}
// German
function compareAccents_de($a, $b) {
return compareAccents($a, $b, 'de');
}
/*
* comparison function to group together accented letters with
* their lower-case non-accented counterparts
* the $lang parameter enables sorting rules for that language
* (ISO 639-1 code)
*/
function compareAccents($a, $b, $lang=null) {
$anew = strtolower(collatedLower($a,$lang));
$bnew = strtolower(collatedLower($b,$lang));
if ($anew < $bnew) return -1;
if ($anew > $bnew) return 1;
return 0;
}
// usage:
usort($myList,'compareAccents_fr');
// to be compared to:
setlocale(LC_COLLATE,'fr');
usort($myList, 'strcoll');
?>
----
Manual Page -- http://www.php.net/manual/en/function.usort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50607
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50607&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50607&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50607&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50607&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50607&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50607&report=yes
Search -- http://master.php.net/manage/user-notes.php