View previous topic :: View next topic |
Author |
Message |
basstech
Joined: 14 Sep 2003 Posts: 17
|
Posted: Sat Sep 20, 2003 10:57 pm Post subject: searchword highlight hack (not javascript) |
|
|
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 |
|
 |
basstech
Joined: 14 Sep 2003 Posts: 17
|
Posted: Sun Sep 21, 2003 6:18 am Post subject: |
|
|
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 |
|
 |
Viper007Bond
Joined: 15 Aug 2003 Posts: 266 Location: Portland, Oregon, USA
|
Posted: Mon Sep 22, 2003 9:54 am Post subject: |
|
|
Mmm, sounds good. I'll have to implement this into my site when I get a chance!  _________________ 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 |
|
 |
MiniSizeIt
Joined: 02 Nov 2002 Posts: 70
|
Posted: Sun Sep 28, 2003 5:21 am Post subject: |
|
|
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 |
|
 |
basstech
Joined: 14 Sep 2003 Posts: 17
|
Posted: Sun Sep 28, 2003 7:41 pm Post subject: |
|
|
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 |
|
 |
basstech
Joined: 14 Sep 2003 Posts: 17
|
Posted: Mon Sep 29, 2003 2:50 pm Post subject: |
|
|
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 |
|
 |
Sansnom
Joined: 31 Jan 2002 Posts: 94 Location: Paris
|
Posted: Sun Oct 05, 2003 8:32 pm Post subject: |
|
|
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 |
|
 |
Sansnom
Joined: 31 Jan 2002 Posts: 94 Location: Paris
|
Posted: Sun Oct 05, 2003 9:17 pm Post subject: |
|
|
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 |
|
 |
Sansnom
Joined: 31 Jan 2002 Posts: 94 Location: Paris
|
Posted: Sun Oct 05, 2003 10:08 pm Post subject: |
|
|
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 |
|
 |
Sansnom
Joined: 31 Jan 2002 Posts: 94 Location: Paris
|
Posted: Mon Oct 06, 2003 1:45 am Post subject: |
|
|
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 |
|
 |
basstech
Joined: 14 Sep 2003 Posts: 17
|
Posted: Thu Oct 09, 2003 10:03 pm Post subject: |
|
|
I didn't see it break any img tags....
I'll look into the case thing. |
|
Back to top |
|
 |
basstech
Joined: 14 Sep 2003 Posts: 17
|
Posted: Thu Oct 09, 2003 10:23 pm Post subject: |
|
|
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 |
|
 |
Sansnom
Joined: 31 Jan 2002 Posts: 94 Location: Paris
|
|
Back to top |
|
 |
|