// helper function to sort matched pages alphabetically by title function by_title($a, $b) { return $a[1] == $b[1] ? strcmp($a[0], $b[0]) : $a[1] > $b[1]; } // SiteSearch object to do the searching $search = new SiteSearch(); // array to hold the pages that match the search term $matching_pages = array(); // directories underneath the document root to search $search_dirs = array('sports', 'movies', 'food'); // regular expression to use in searching files. The "S" pattern // modifier tells the PCRE engine to "study" the regex for greater // efficiency. $search->bodyRegex = '#<body>(.*' . preg_quote($_GET['term'], '#') . '.*)</body>#Sis'; // add the files that match in each directory to $matching pages foreach ($search_dirs as $dir) { $matching_pages = array_merge($matching_pages, $search->searchDir($_SERVER['DOCUMENT_ROOT'] . '/' . $dir)); } if (count($matching_pages)) { // sort the matching pages by title usort($matching_pages, 'by_title'); print '<ul>'; // print out each title with a link to the page foreach ($matching_pages as $k => $v) { print sprintf('<li> <a href="%s">%s</a>', $v[0], $v[1]); } print '</ul>'; } else { print 'No pages found.'; }