note 77654 added to function.usort

From: Date: Thu, 06 Sep 2007 23:59:47 +0000
Subject: note 77654 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-130280@lists.php.net to get a copy of this message
With this function you'll be able to sort words with international latin characters such as 'á', 'é', 'í', etc. <?php /************************************************************* SORT INTERNATIONAL LATIN CHARACTERS AS 'á', 'é', 'í', etc. Felipe Brahm http://www.felipebrahm.com IMPORTANT! You must work with UNICODE files to use this script as is. **************************************************************/ //convert international latin characters to "simple" ones //this is used so that "a" is read as "á" when comparing strings function icString($string) { $a = array("à","á","â","ã","ä","å"); $e = array("è","é","ê","ë"); $i = array("ì","í","î","ï"); $o = array("ò","ó","ô","õ","ö"); $u = array("ù","ú","û","ü"); $n = array("ñ"); $string = str_replace($a, 'a', $string); $string = str_replace($e, 'e', $string); $string = str_replace($i, 'i', $string); $string = str_replace($o, 'o', $string); $string = str_replace($u, 'u', $string); $string = str_replace($n, 'n', $string); return $string; } function toLower($string) { if(function_exists('mb_strtolower')) return mb_strtolower($string, mb_detect_encoding($string)); return strtolower($string); } function compareWords($x, $y) { if (icString(toLower($x)) == icString(toLower($y))) return 0; else if(icString(toLower($x)) < icString(toLower($y))) return -1; else return 1; } /**************************************** THIS IS THE FUNCTION YOU SHOULD CALL *****************************************/ function sortLatinCharacters(&$array) { usort($array, 'compareWords'); } ?> Now an example: <?php /* ======================== USAGE EXAMPLE. ======================== If you have the following array: */ $words = array('Felipe', 'Brahm', 'Naranja', 'Ñandú', 'Ángeles', 'Árbol', 'Amigo'); /* ============ THEN: ============ */ sort($words); print_r($words); /* Will output: Array ( [0] => Amigo [1] => Brahm [2] => Felipe [3] => Naranja [4] => Ángeles [5] => Árbol [6] => Ñandú ) */ /* ============ AND: ============ */ sortLatinCharacters($words); print_r($words); /* Will output: Array ( [0] => Amigo [1] => Ángeles [2] => Árbol [3] => Brahm [4] => Felipe [5] => Ñandú [6] => Naranja ) */ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 190.45.243.147 ---- Manual Page -- http://www.php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/77654 Del: integrated -- https://master.php.net/note/delete/77654/integrated Del: useless -- https://master.php.net/note/delete/77654/useless Del: bad code -- https://master.php.net/note/delete/77654/bad+code Del: spam -- https://master.php.net/note/delete/77654/spam Del: non-english -- https://master.php.net/note/delete/77654/non-english Del: in docs -- https://master.php.net/note/delete/77654/in+docs Del: other reasons-- https://master.php.net/note/delete/77654 Reject -- https://master.php.net/note/reject/77654 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130280) next »