note 50607 added to function.usort

From: 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( "/&aacute;|&agrave;|&acirc;|&atilde;|&aring;| &Aacute;|&Agrave;|&Acirc;|&Atilde;|&Aring;/", "/&auml;|&aelig;|&Auml;|&AElig;/", "/&ccedil;|&Ccedil;/", "/&eacute;|&egrave;|&ecirc;|&euml;| &Eacute;|&Egrave;|&Ecirc;|&Euml;/", "/&igrave;|&iacute;|&icirc;|&iuml;| &Igrave;|&Iacute;|&Icirc;|&Iuml;/", "/&ntilde;|&Ntilde;/", "/&ograve;|&oacute;|&ocirc;|&otilde;|&oslash;| &Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Oslash;/", "/&ouml;|&oelig;|&Ouml;|&OElig;/", "/&szlig;/", "/&ugrave;|&uacute;|&ucirc;| &Ugrave;|&Uacute;|&Ucirc;/", "/&uuml;|&Uuml;/", "/&yacute;|&yuml;|&Yacute;/"); $replace = array('a', 'ae', 'c', 'e', 'i', 'n', 'o', 'oe', 'ss', 'u', 'ue', 'y'); break; default: // 'en' (English), 'fr' (French) $patterns = array( "/&aacute;|&agrave;|&acirc;|&atilde;|&auml;|&aring;| &Aacute;|&Agrave;|&Acirc;|&Atilde;|&Auml;|&Aring;/", "/&aelig;|&AElig;/", "/&ccedil;|&Ccedil;/", "/&eacute;|&egrave;|&ecirc;|&euml;| &Eacute;|&Egrave;|&Ecirc;|&Euml;/", "/&igrave;|&iacute;|&icirc;|&iuml;| &Igrave;|&Iacute;|&Icirc;|&Iuml;/", "/&ntilde;|&Ntilde;/", "/&ograve;|&oacute;|&ocirc;|&otilde;|&ouml;|&oslash;| &Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Ouml;|&Oslash;/", "/&oelig;|&OElig;/", "/&szlig;/", "/&ugrave;|&uacute;|&ucirc;|&uuml;| &Ugrave;|&Uacute;|&Ucirc;|&Uuml;/", "/&yacute;|&yuml;|&Yacute;/"); $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

« previous php.notes (#85923) next »