View previous topic :: View next topic |
Author |
Message |
Cyberian75
Joined: 26 Sep 2002 Posts: 1285 Location: Oregon
|
Posted: Fri Nov 19, 2004 4:34 am Post subject: [MOD] Improved Search |
|
|
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.
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 .= ')';
}
|
And add the following to the search form:
Code: |
<input type="checkbox" name="exact" value="1">exact word match<br>
<input type="radio" name="scond" value="AND" checked>AND
<input type="radio" name="scond" value="OR">OR
|
Lastly, if you want to highlight searched words, replace "the_content" function in the "b2template_functions.php" with the following:
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 = "<span class = \"search\">".$word."</span>";
$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;
}
|
EDIT: Seperate words with spaces.  _________________ Michael P.

Last edited by Cyberian75 on Fri Nov 19, 2004 8:58 pm; edited 7 times in total |
|
Back to top |
|
 |
Sigg3
Joined: 03 Jul 2003 Posts: 906 Location: Oslo, Norway
|
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1285 Location: Oregon
|
Posted: Fri Nov 19, 2004 9:11 am Post subject: |
|
|
Sigg3 wrote: | Is this like google's search, like you have to enclose it in quotation marks?? |
You seperate words with spaces, not quotes.
Sigg3 wrote: | And: This is for the b2 form, right? Not the b2edit form? Both? |
Both -- if you put that "exact word match" input tag in the edit page.
 _________________ Michael P.
 |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1285 Location: Oregon
|
Posted: Fri Nov 19, 2004 7:06 pm Post subject: |
|
|
Bugs fixed.  _________________ Michael P.
 |
|
Back to top |
|
 |
Sigg3
Joined: 03 Jul 2003 Posts: 906 Location: Oslo, Norway
|
|
Back to top |
|
 |
sh0ck
Joined: 02 May 2004 Posts: 56 Location: Norway
|
Posted: Thu Nov 25, 2004 9:26 am Post subject: |
|
|
Bug: The highlight feature breaks up links with a url matching the search term. _________________ http://www.licklinux.com |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1285 Location: Oregon
|
Posted: Thu Nov 25, 2004 6:44 pm Post subject: |
|
|
I know. You can use strip_tags function to strip HYML tags. _________________ Michael P.
 |
|
Back to top |
|
 |
sh0ck
Joined: 02 May 2004 Posts: 56 Location: Norway
|
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1285 Location: Oregon
|
Posted: Thu Dec 02, 2004 5:26 pm Post subject: |
|
|
Code: | $content = strip_tags($content); |
_________________ Michael P.
 |
|
Back to top |
|
 |
|