Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

Highlight search query in results page?


Reply


25 Posts   7811 Views

Avatar
SilverRay

Community Member, 167 Posts

23 July 2009 at 8:17pm

True ;)

I'll try tomorrow and post back!

Avatar
SilverRay

Community Member, 167 Posts

28 July 2009 at 9:12pm

Edited: 28/07/2009 9:12pm

OK, Sorry for the delay (am very busy at the moment)... just tried to implement your code, pasted it literally, but got a blank page. That does not mean there's something wrong with your example, probably it doesn't sit well within my site environment, so I will try to sort it out. Pretty busy, so not sure when, but I will. Thanks, btw.

Avatar
Juanitou

Community Member, 323 Posts

28 July 2009 at 9:30pm

Hi!

I assume you flushed the cache and all that… Also, check that you are using the same 'Search' param in $_REQUEST['Search'].

Hope it works,
Juan

Avatar
Ultimate

Community Member, 18 Posts

11 August 2009 at 8:29am

Hey Joanitou, I've implemented your code and it works pretty well. But there's still a problem. If higlighting is enabled the follwing code:

if($highlight) {
// Add a span around all key words from the search term as well
if($stringPieces) foreach($stringPieces as $stringPiece) {
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);

... will replace the original string with the entered search-string. If case there was an uppercase string and you've saerched for the same word, but lowercase, the uppercase string will be substituted by the entered string. The code should be altered in the way, that the span-tag will only be added around the found string, without further changes to the original text.

Avatar
Juanitou

Community Member, 323 Posts

11 August 2009 at 10:38pm

Hi!

It's not ‘my code’, I copied it from sapphire framework. I don't really know how to do what you suggest, I hope somebody more proficient in PHP than me will find a way.

Thanks for the interest,
Juan

Avatar
Ultimate

Community Member, 18 Posts

11 August 2009 at 11:56pm

Ok sorry. So maybe I will have some time this evening to fix it. I'll keep you posted.

Avatar
Juanitou

Community Member, 323 Posts

12 August 2009 at 12:59am

That would be great! You could post the code in Trac as an enhancement to this sapphire function (ContextSummary).

Avatar
Ultimate

Community Member, 18 Posts

13 August 2009 at 8:53am

So i finally got some time. Changed the code, so that the original text won't be altered. Moreover summary won't clip inside a word at the end. $characters will be increased until a blank space is found.

function MyContextSummary($characters = 500, $string = false, $striphtml = true, $highlight = true)
      {

   if(!$string) $string = $_REQUEST['Search']; // Use the default "Search" request variable (from SearchForm)

      /*** Prepare Content ***/

      // Replace <br /> in order to get separate words

      $content = str_replace('<br />', ' ', $this->Content);

      // Decoding entities prevents XML validation error

      $content = html_entity_decode($content, ENT_COMPAT , 'UTF-8');

      // Remove HTML tags so we don't have to deal with matching tags

      $text = strip_tags($content);

      // Remove BBCode

      $pattern = '|[[\/\!]*?[^\[\]]*?]|si';

      $replace = '';

      $text = preg_replace($pattern, $replace, $text);

      // Find the search string

      $position = (int) stripos($text, $string);

      // We want the search string to be in the middle of our block to give it some context

      $position = max(0, $position - ($characters / 2));

      // We don't want to start mid-word

      if($position > 0)
         {

      $position = max((int) strrpos(substr($text, 0, $position), ' '), (int) strrpos(substr($text, 0, $position), "\n"));

         }
         
      $summary = substr($text, $position, $characters);
      
      // We also don't want to end mid-word
      $offset = $characters+$position;
      if ($offset > strlen($text)-1) $offset = strlen($text)-1;
      $position = min((int) strpos($text, ' ', $offset), (int) strpos($text, "\n", $offset));
      if ($position) $summary = $summary.substr($text, $offset, $position-$offset);
          
   if($highlight)
      {
      // Setting the content taht will be inserted before and after the highlighted words
      $before_content = "<span class=\"highlight\">";
      $after_content = "</span>";
      
      // Save the different search values into an array
      $stringPieces = explode(' ', $string);
      
      foreach($stringPieces as $stringPiece)
         {
         //Setting the start from where $stringPiece will be searched
         $offset = 0;
         // Recursively add before_text and after_text around all found $stringPiece

            while($position = stripos($summary, $stringPiece, $offset))
               {
               $summary =
                  substr($summary, 0, $position)
                  .$before_content
                  .substr($summary, $position, strlen($stringPiece))
                  .$after_content
                  .substr($summary, $position + strlen($stringPiece), strlen($summary)-($position+1));
               
               $offset = $position + strlen($before_content) + strlen($after_content);
            }
         }
      }
      
      return trim($summary);
      }


I hope there won't be any bugs, but feel free to report to this thread.