Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Highlight search query in results page?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3 4
Go to End
Author Topic: 7355 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Highlight search query in results page? Link to this post

    True ;)

    I'll try tomorrow and post back!

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Highlight search query in results page? Link to this post

    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.

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Highlight search query in results page? Link to this post

    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

  • Ultimate
    Avatar
    Community Member
    18 Posts

    Re: Highlight search query in results page? Link to this post

    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.

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Highlight search query in results page? Link to this post

    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

  • Ultimate
    Avatar
    Community Member
    18 Posts

    Re: Highlight search query in results page? Link to this post

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

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Highlight search query in results page? Link to this post

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

  • Ultimate
    Avatar
    Community Member
    18 Posts

    Re: Highlight search query in results page? Link to this post

    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.

    7355 Views
Page: 1 2 3 4
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.