Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:39:03+00:00 2026-05-27T14:39:03+00:00

Am playing around with the standard WordPress search and am using this code in

  • 0

Am playing around with the standard WordPress search and am using this code in the functions file to highlight searched terms in the displayed results content.

function search_content_highlight() {$content = get_the_content();
$keys = implode('|', explode(' ', get_search_query()));
$content = preg_replace
('/(' . $keys .')/iu', '<strong class="search-  highlight">\0</strong>', $content);
echo '<p>' . $content . '</p>';
}

Am using the content rather than excerpt so it always actually shows the needed word, but Id really love to trim the content, so it was just a dozen or so words either side of the searched word, which is held in the strong tags in the above code. Am pretty new to all this but am hoping someone can point me in the right direction if such a trim is possible.

Thanks in advance for any help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:39:04+00:00Added an answer on May 27, 2026 at 2:39 pm

    I am guessing that you are going to want to show a minimum of all of the bolded words. To do that you need to find where you find the first and last instance of a matched word.

    function search_content_highlight()
    {
        $content = get_the_content();
        $keysArray = explode(' ', get_search_query());
        $keys = implode('|', $keysArray);
        $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content);
        $minLength = 150; //Number of Characters you want to display minimum 
        $start = -1;
        $end = -1;
        foreach($keysArray as $term)
        {
            $pos = strpos($content, $term);
            if(!($pos === false))
            {
                if($start == -1 || $pos<$start)
                    $start = $pos-33; //To take into account the <strong class="search-highlight">
                if($end == -1 || $pos+strlen($term)>$end)
                    $end = $pos+strlen($term)+9; //To take into account the full string and the </strong>
            }
        }
        if(strlen($content) < $minLength)
        {
            $start = 0;
            $end = strlen($content);
        }
        if($start == -1 && $end == -1)
        {
            $start =0;
            $end = $minLength;
        }
        else if($start != -1 && $end == -1)
        {
            $start = ($start+$minLength <= strlen($content))?$start:strlen($content)-$minLength;
            $end = $start + $minLength;
        }
        else if($start == -1 && $end !=-1)
        {
            $end = ($end-$minLength >= 0)?$end:$minLength;
            $start = $end-$minLength;
        }
        echo "<p>".(($start !=0)?'...':'').substr($content,$start,$end-$start).(($end !=strlen($content))?'...':'')."</p>";   
    }    
    

    I have tested the above code and it works. You might want to consider adding more logic in to have a max description size

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

System Specs and task I am using Code::Blocks on Ubuntu 10.10 and playing around
While playing around with regexps in Scala I wrote something like this: scala> val
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
I'm playing around with LinqToSQL using an existing multi-lingual database, but I'm running into
Im new to this android development, and im currently playing around with the HelloM4A
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
Playing around with generating text randomly with each page refresh using php. Is there
Just playing around with java trying to learn it etc. Here is my code
I am playing around with the auto feature in the C++0X standard but I
playing around with a property file i figured that there seems to be a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.