How can I show the actual “search term” from the content in the post page and display it in wordpress?

I want to show on the search results page, a sentence or few words where the search term is actually located within the content in the post and display it and underline the search term.

For the title I have done similar like this:

Read More
<?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<u class="search-excerpt"></u>', $title); ?>
                <h3 class="test"><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a></h3>

How can I show approx 15 words or so from the actual “search term” from the post content page and display it?

for example I want to make similar:

Search term = omnis

Show this = Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque

Thanks a bunch,

Related posts

Leave a Reply

2 comments

  1. Kind of found one answer here:
    http://atastypixel.com/blog/keeping-blog-visitors-by-showing-meaningful-search-results-in-wordpress/

    <?php
    // Configuration
    $max_length = 400; // Max length in characters
    $min_padding = 30; // Min length in characters of the context to place around found search terms
    
    // Load content as plain text
    global $wp_query, $post;
    $content = (!post_password_required($post) ? strip_tags(preg_replace(array("/r?n/", '@<s*(p|brs*/?)s*>@'), array(' ', "n"), apply_filters('the_content', $post->post_content))) : '');
    
    // Search content for terms
    $terms = $wp_query->query_vars['search_terms'];
    if ( preg_match_all('/'.str_replace('/', '/', join('|', $terms)).'/i',             $content, $matches, PREG_OFFSET_CAPTURE) ) {
    $padding = max($min_padding, $max_length / (2*count($matches[0])));
    
      // Construct extract containing context for each term
      $output = '';
      $last_offset = 0;
      foreach ( $matches[0] as $match ) {
        list($string, $offset) = $match;
        $start  = $offset-$padding;
        $end = $offset+strlen($string)+$padding;
        // Preserve whole words
        while ( $start > 1 && preg_match('/[A-Za-z0-9'"-]/', $content{$start-1}) ) $start--;
        while ( $end < strlen($content)-1 && preg_match('/[A-Za-z0-9'"-]/', $content{$end}) ) $end++;
        $start = max($start, $last_offset);
            $context = substr($content, $start, $end-$start);
        if ( $start > $last_offset ) $context = '...'.$context;
        $output .= $context;
        $last_offset = $end;
      }
    
      if ( $last_offset != strlen($content)-1 ) $output .= '...';
    } else {
      $output = $content;
    }
    
    if ( strlen($output) > $max_length ) {
      $end = $max_length-3;
      while ( $end > 1 && preg_match('/[A-Za-z0-9'"-]/', $output{$end-1}) ) $end--;
      $output = substr($output, 0, $end) . '...';
    }
    
    // Highlight matches
    $context = nl2br(preg_replace('/'.str_replace('/', '/', join('|', $terms)).'/i', '<strong>$0</strong>', $output));
    ?>
    
    <p class="search_result_context">
      <?php echo $context ?>
    </p>
    
  2. If you want to use a plugin Relevanssi allows you to use a custom snippet for the returned results, for which you can set a length and choice of highlight for the searched-term.