On Tue, Nov 18, 2003 at 01:15:32PM +0100, Adam i Agnieszka Gasiorowski FNORD wrote: : : There is an array of regexes, for example : : $array = array('moon', '[wh]ood', '[^as]eed' ... : (about 300 entries). : : I want to sort it comparing to the : character lenght of a regex. For example : [wh]ood is 4 characters, moon is 4 characters. : There are only letters of the alphabet and : letter ranges present in those regexes. I : want the "longest" ones first. : : How would you write the sorting function?
This might be the most functionally correct, although it's definitely not the fastest route. function re_len($pat) { return strlen(preg_replace('/\[[^]]+]/', '_', $pat)); } function re_sort($a_pat, $b_pat) { $a = re_len($a_pat); $b = re_len($b_pat); if ($a == $b) { return 0; } return ($a < $b ) ? -1 : 1; } usort($array, 're_sort'); BTW, re_len() will bomb on certain oddball patterns with strange ranges. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php