boardom Forum Index boardom
b2 message board
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

searchword highlight hack (not javascript)

 
Post new topic   Reply to topic    boardom Forum Index -> Hacks
View previous topic :: View next topic  
Author Message
basstech



Joined: 14 Sep 2003
Posts: 17

PostPosted: Sat Sep 20, 2003 10:57 pm    Post subject: searchword highlight hack (not javascript) Reply with quote

I saw that some people were using a javascript to highlight keywords. I didn't like it though because it highlights the words everywhere on the page.

I decided to write a hack to do the same thing but only on the article text.


This is a simple hack.

Go to around line 294 in your b2-include/b2template.functions.php file.

Look for the "the_content" function.

It should look something like this:

Code:
function the_content($more_link_text='(read more...)', $stripteaser=0, $more_file='') {
   $content = get_the_content($more_link_text,$stripteaser,$more_file);
   $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;
}


Replace the entire function with this:
Code:
function the_content($more_link_text='(read more...)', $stripteaser=0, $more_file='') {
   global $s;
   $searchwords  = explode(" ", $s);
   $content = get_the_content($more_link_text,$stripteaser,$more_file);
   for($i = 0; $i <= count($searchwords); $i++) {
      $content = str_replace($searchwords[$i], "<span class=\"searchword\">".$searchwords[$i]."</span>", $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;
}


There is one last thing left to do. You need to edit your css file and add a class called "searchword".

Here's what mine looks like:
Code:
.searchword {
   background-color: #F90;
}


If you were running the javascript hack you should already have that.

That's all there is to do.

You can see it in action at: http://ravemedia.org/b2
Back to top
View user's profile Send private message Visit poster's website AIM Address
basstech



Joined: 14 Sep 2003
Posts: 17

PostPosted: Sun Sep 21, 2003 6:18 am    Post subject: Reply with quote

Ok....

So I discoverd some serious flaws in my search highlight hack.

I decided to start over from scratch.

One of the problems I discovered was that if the word you searched for was in some html, it tried to highlight it anyway.

Somebody posted a highlighting function in a comment on php.net, so I used it and placed it in a seperate file.

I took the following code and saved it in a file in my include directory called searchhighlight.php
Code:
<?php
function getHTMLHighlight($needle, $haystack, $hlS, $hlE)
{
   $parts = explode(">", $haystack);
    foreach($parts as $key=>$part)
   {
     $pL = "";
       $pR = "";

     if(($pos = strpos($part, "<")) === false)
       $pL = $part;
     elseif($pos > 0)
     {
       $pL = substr($part, 0, $pos);
       $pR = substr($part, $pos, strlen($part));
     }
     if($pL != "")
       $parts[$key] = preg_replace('|\b('.quotemeta($needle).')\b|iU', $hlS.'\\1'.$hlE, $pL) . $pR;
   }
   return(implode(">", $parts));
}
?>


I then added the following at the top of template.functions.php just below the <?php
Code:
// Search word highlight functions
include($b2blah.$b2inc.'/searchhighlight.php');


Then I added the following code in template.functions.php in between $content=$pages[$page-1]; and $content=explode('<!--more-->', $content); around line 355.

Code:
   // highlight searched words
   if (isset($s)&&$s!="") {
      $searchwords  = explode(" ", $s);
      for($i = 0; $i <= count($searchwords); $i++) {
         $content = getHTMLHighlight($searchwords[$i], $content, "<span class=\"searchword\">", "</span>");
      }
   }


This should be all you have to do.

Optionally though you may add some more code so that when someone searches, and when one of the searched posts has a more link it will highlight the text on the page if they click the more link.

What I did was change this line:
Code:
         $output .= ' <a href="'.$file.$querystring_start.'p'.$querystring_equal.$id.$querystring_separator.'more'.$querystring_equal.'1#more'.$id.'">'.$more_link_text.'</a>';


to
Code:
         $output .= ' <a href="'.$file.$querystring_start.'p'.$querystring_equal.$id.$querystring_separator.'more'.$querystring_equal.'1'.$querystring_separator.'s'.$querystring_equal.$s.'#more'.$id.'">'.$more_link_text.'</a>';


You can find it around line 375.

An example of search that would use this is here: http://ravemedia.org/b2/index.php?s=firestorm&submit=search

If you click the read more link, the text on the following page that displays the whole article should be highlighted too.

Please note I'm not trying to release any official hacks or anything, I just thought I would share my own hacks so others don't have to reinvent the wheel.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Viper007Bond



Joined: 15 Aug 2003
Posts: 266
Location: Portland, Oregon, USA

PostPosted: Mon Sep 22, 2003 9:54 am    Post subject: Reply with quote

Mmm, sounds good. I'll have to implement this into my site when I get a chance! Very Happy
_________________
http://www.viper007bond.com

If you haven't already installed b2, I advise you look into WordPress or b2evo instead as b2 is dead.
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
MiniSizeIt



Joined: 02 Nov 2002
Posts: 70

PostPosted: Sun Sep 28, 2003 5:21 am    Post subject: Reply with quote

I was really interested in this hack, and tried installing it.

I got no errors, and it plan out didn't work.

Not sure what happened.
Back to top
View user's profile Send private message
basstech



Joined: 14 Sep 2003
Posts: 17

PostPosted: Sun Sep 28, 2003 7:41 pm    Post subject: Reply with quote

Did you put the css in your css file? That could be the problem.

If you post a link to your site, I'll take a look and see what's going on in your html and css.
Back to top
View user's profile Send private message Visit poster's website AIM Address
basstech



Joined: 14 Sep 2003
Posts: 17

PostPosted: Mon Sep 29, 2003 2:50 pm    Post subject: Reply with quote

An optional thing I did on my site was to place this code in my index.php

Code:
<?
      if (isset($s)&&$s!="") {
         ?><h4>Search results for: </h4><?
         $searchwords = explode(" ", $s);
         $numwords = count($searchwords);
         foreach($searchwords as $key => $searchword) {
            echo "\"".$searchword."\" ";
            if ($key != ($numwords -1)) {
               echo " & ";
            }
         }
      } ?>


This just places "Search results for:" and the searchwords used in your page. I placed it just above the articles on my site, although it could be placed just about anywhere in your index.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Sansnom



Joined: 31 Jan 2002
Posts: 94
Location: Paris

PostPosted: Sun Oct 05, 2003 8:32 pm    Post subject: Reply with quote

I have done everything but it doesn't work.
Have you got an idea ?

It is for : http://www.sansfin.com

Perhaps the css is hide by first css on posts... ?
_________________
http://www.sansfin.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Sansnom



Joined: 31 Jan 2002
Posts: 94
Location: Paris

PostPosted: Sun Oct 05, 2003 9:17 pm    Post subject: Reply with quote

It works !!!!!!!!!!!!!!!
Great ! Thankx ! Youhouuuu !

(you should say that we must make ALL the modifications you explain : the first hack AND the corrections. Because when we read the forum, it seems that you say we must begin at zero whith your second post on the forum !).

Thankx for all anyway !!!
_________________
http://www.sansfin.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Sansnom



Joined: 31 Jan 2002
Posts: 94
Location: Paris

PostPosted: Sun Oct 05, 2003 10:08 pm    Post subject: Reply with quote

Just one bug : when the word is in a <img> tag, it highlights it and breaks the tag, so the image isn't visible and you obtain a half of tag instead. ugly bug !
_________________
http://www.sansfin.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Sansnom



Joined: 31 Jan 2002
Posts: 94
Location: Paris

PostPosted: Mon Oct 06, 2003 1:45 am    Post subject: Reply with quote

Another bug : it is case sensitive, so when you search one word, b2 gives you all posts (no case sensitive) but highlights are only in some posts (case sensitive)...
You search "coucou", you obtain with b2 posts with "coucou" and "COUCOU" or "Coucou", but only posts with "coucou" are highlighted.

... And I ignore how to fix this because it's chinese for me !
_________________
http://www.sansfin.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
basstech



Joined: 14 Sep 2003
Posts: 17

PostPosted: Thu Oct 09, 2003 10:03 pm    Post subject: Reply with quote

I didn't see it break any img tags....

I'll look into the case thing.
Back to top
View user's profile Send private message Visit poster's website AIM Address
basstech



Joined: 14 Sep 2003
Posts: 17

PostPosted: Thu Oct 09, 2003 10:23 pm    Post subject: Reply with quote

It matches uper case and lower case on my site. I'm not sure why it's not doing matching mixed cases on yours.

I didn't write the function that does the highlighting myself...

Code:
function getHTMLHighlight($needle, $haystack, $hlS, $hlE)
{
   $parts = explode(">", $haystack);
    foreach($parts as $key=>$part)
   {
     $pL = "";
       $pR = "";

     if(($pos = strpos($part, "<")) === false)
       $pL = $part;
     elseif($pos > 0)
     {
       $pL = substr($part, 0, $pos);
       $pR = substr($part, $pos, strlen($part));
     }
     if($pL != "")
       $parts[$key] = preg_replace('|\b('.quotemeta($needle).')\b|iU', $hlS.'\\1'.$hlE, $pL) . $pR;
   }
   return(implode(">", $parts));
}


I just wrote the stuff to make it work inside of b2.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Sansnom



Joined: 31 Jan 2002
Posts: 94
Location: Paris

PostPosted: Sat Oct 11, 2003 4:22 pm    Post subject: Reply with quote

If you go to :

http://www.sansfin.com/index.php?s=sauvage&paged=2

You will see in the last post that "SAUVAGE" isn't highlighted.

If you go to :

http://www.sansfin.com/index.php?s=rouge

You will see it breaks the img tag to show (but doesn't highlight) the word inside (here : "rouge").

I don't understand. I have exactely do what you explain... I use b2 0.6.2...
_________________
http://www.sansfin.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    boardom Forum Index -> Hacks All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2 © 2001, 2002 phpBB Group