# MOD: Improved Search # by Cyberian75 Source: http://cafelog.com/board/viewtopic.php?t=5924 I've modified the search "engine" using the REGEXP operator; therefore, it now can search for exact matches which can lessen the workload of the database. STEP 1 In the "blog.header.php" file, replace the "search" section with the following: [Code] // if a search pattern is specified, load the posts that match if (!empty($s)) { //gets rid of non-alphanumeric characters excluding spaces $s = eregi_replace("[^a-z0-9\ ]+", " ", $s); $s_array = explode(" ", trim($s)); $search .= ' AND ('; for ($i = 0; $i < count($s_array); $i++) { if ($i > 0) { $search .= ' '.$scond.' '; } if ($exact) { $search .= '(post_title REGEXP \'[[:<:]]'.$s_array[$i].'[[:>:]]\' OR post_content REGEXP \'[[:<:]]'.$s_array[$i].'[[:>:]]\')'; } else { $search .= '(post_title LIKE \'%'.$s_array[$i].'%\' OR post_content LIKE \'%'.$s_array[$i].'%\')'; } } $search .= ')'; } [/code] STEP 2 And add the following to the search form: [code] exact word match
AND OR [/code] ALTERNATE STEP: Lastly, if you want to highlight searched words, replace "the_content" function in the "b2template_functions.php" with the following: Code: [code] function the_content($more_link_text='(more...)', $stripteaser=0, $more_file='') { global $HTTP_GET_VARS, $s_array; $content = get_the_content($more_link_text,$stripteaser,$more_file); if(isset($HTTP_GET_VARS["s"]) && count($s_array) > 0) { $s = $HTTP_GET_VARS["s"]; $exact = $HTTP_GET_VARS["exact"]; foreach ($s_array AS $word) { if ($exact) { $search = "[[:<:]]".$word."[[:>:]]"; } else { $search = $word; } $replace = "".$word.""; $content = eregi_replace($search, $replace, $content); } } $content = convert_bbcode($content); $content = convert_gmcode($content); $content = convert_smilies($content); $content = convert_chars($content, 'html'); $content = apply_filters('the_content', $content); echo $content; } [/code] Seperate words with spaces when you search. Enjoy! Source: http://cafelog.com/board/viewtopic.php?t=5924