Recent Topics

highlight search word

Started by on Oct 09, 2003 – Contents updated: Oct 09, 2003

Oct 09, 2003 17:16    

highlighting the searchword was available through a hack in b2
is there such a thing in b2evo or do you need to hack it? :)

Oct 09, 2003 23:11

It's not there at the moment, but since I'm not writing the software (just testing it), I've no idea when Francois is planning on implementing it (if he is at all).

Oct 13, 2003 23:38

yes please!
I used to use yours with b2 :D

Oct 13, 2003 23:46

Ok...

Sorry this is a little late, but here's how to do it in b2evo.

Open up blogs/b2evocore/_functions_bposts

Near the top below

Code

if( $use_textile ) require_once (dirname(__FILE__).'/_functions_textile.php');

Add

Code

require_once (dirname(__FILE__).'/searchhighlight.php');

Around line 535 look for ' echo $content; ' just before it but after the last '}' add this code:

Code

//search highlight hack
  if (isset($s)&&$s!="") {
    $searchwords  = explode(" ", $s);
    $class = 1;
    for($i = 0; $i <= count($searchwords); $i++) {
      $content = getHTMLHighlight($searchwords[$i], $content, "<span class=\"searchword".$class."\">", "</span>");
      if ($class == 1) {
        $class = 2;
      } else {
        $class = 1;
      }
    }
  }

Now create a file in blogs/b2evocore/ called searchhighlight.php

Copy and Paste the following code into it.

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));
}
?>

This code highlights searchwords two different colors defined in your css.

the classes for the css are: searchword1 and searchword2.

It will alternate between colors, so if you search for 3 words the first will be color 1, the second color 2, and the third word will be color 1.

If you want all the words the same color, just set searchword1 and searchword2 to be the same.

Here: is the css I use on www.team-geek.net

Code

.searchword1 {
  background-color: #F90;
  color: #06C;
}
 
.searchword2 {
  background-color: #06C;
  color: #F90;
}

Oct 14, 2003 11:26

this oughta be incorporated in b2evo! :D

But, I can not make it work. I've done everything you said, but still http://republicum.se shows nothing when I search for a word :(

Don't mind it being in swedish ;)

Oct 14, 2003 15:59

I took a look. It's not working. Could you do me a favor. Could you make a copy of _functions_bposts.php and name it _functions_bposts.phps.

I'll look at it to make sure you added the code in the right place. That's the only thing I can think of that is wrong.

Oct 14, 2003 18:29

Ok....

It was my fault. I left out an essential piece of code.

You need to go upto to top of the the_content() function.

Just below the function declaration (should be line 515) replace:

Code

global $use_textile;

Code

global $use_textile, $s;
This is essential for the function to see the searchwords because they are contained in $s.

It should work after that, as you placed the rest of the code in the right places.

Oct 14, 2003 21:14

it gets all fucked up :/
and the åäö gets decoded too. hmm :(

Oct 14, 2003 21:36

I haven't used it with any other languages.

I'm going to take a look at your site and see what it's doing.

Oct 14, 2003 21:59

Ok....

I'm gonna have to look into this.

For some reason it's getting false positives on nothing. I never noticed it because my css only highlights. It doens't do any boxes or borders. For the time being, if you removed the margins, padding, and border from you searchwards classes, it will look alot better. Hopefully I'll have an answer by this evening.

Oct 14, 2003 22:28

it wont search backwards either.
hmm, maybe i'll just remove the code until it's incorporated into b2evo.
Can't figure shit out. the php looks okay though.

great job!

Oct 14, 2003 23:02

Grrr...
I figured out the problem.

Code

for($i = 0; $i <= count($searchwords); $i++) {

Should be:

Code

for($i = 0; $i < count($searchwords); $i++) {

I don't know about the character encoding.

I'm american, and have never done any coding with other languages then english.

Let me know how it turns out.

I'll be glad to help.

Oct 14, 2003 23:12

I figured I'd share this too. I have this code at the top of my _main.php right before the posts start.

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 " & ";
        }
      }
    }
?>

Demo: http://www.team-geek.net/basstech.php?s=the+kitten+blog+b2evo&sentence=OR&submit=Search

Oct 14, 2003 23:59

it's working now! thanks.
Now to understand why my categories are bugging and why the search wont search older posts :(

Oct 15, 2003 03:20

I don't know anything about it not searching older posts. Both my blogs are pretty new. I haven't experienced that with mine.

Oct 18, 2003 17:19

Sure no problem. At first glance I don't see anything wrong. I'll get back to you on it in a bit

Oct 19, 2003 00:19

thanks a million. hmm
lemme update that phps :)
it'll be done 2 secs after this post :P

the search acts funny.
just try to copypaste words from older posts and search them from the front page.

also, when me and my girlfriend search simultaneously we got different results on the same search.

it's really weird

Oct 19, 2003 06:16

I didn't experince any problems finding older posts.

It looks like you turned the highlight off though.

I'm sure if it might be a text encoding thing. Your one post per page layout makes searches kind of restrictive, and I think the problem might be that. If you search for something that is in both an older and a newer post, you only going to see the newest post, Also finding no posts breaks your layout just a little bit. You might want to use css to force the height of your layout div's to be 100% or something.

Oct 19, 2003 23:10

i have 2days of posts shown on my site, and yeah i turned highlight off because sometimes it highlighted and sometimes it didn't.

height:100% on a div-layer breaks weirdly in firebird and ie, so i have 500px padding-bottom instead.

as you can see nav_link doesn't show either.
something is wrong :/

thanks so far :D

Jul 22, 2005 21:58

Any news on this? As the hack is two years old... will it work in the newest version? Thanks


Form is loading...

powered by b2evolution – This forum is powered by b2evolution CMS, a complete engine for your website.