Hi all,

I'm currently putting together a search engine for my website, and so far, everything is going pretty smoothly.

The only thing left that I would like to do is when the search results are displayed, I want to be able to highlight the search term entered in the page that displays the results of the query.

For example, if I search for the term "dog grooming", here is a possible result, with the term "dog grooming" bolded:

"Acme Dog Grooming offers the finest and most affordable dog grooming service in your area. Contact us at (555)123-4567 for more details".

Below is a class that I am using, and would like to know how I can modify it so that I can highlight the search term entered. In the code below, the variable $_POST['searchbox'] is the search term being entered and what I'd like to hightlight.

class SearchAboutMe extends Search{
        public function __construct()
        {

            $db = new PDO(### Database connection details ###);
            $q = $db->prepare(" SELECT something FROM somewhere WHERE somecolumn LIKE ? ");
            $q->execute(array('%'.$_POST['searchbox'].'%')); ### Here the query is being executed
            $q->setFetchMode(PDO::FETCH_ASSOC);

            $r = $q->fetch();
            $this->username = $r['username'];
            $this->city = $r['city'];
            $this->state = ucwords(strtolower($r['state']));
            $this->zipcode = $r['zip'];
            $this->info = $r['info'];

            ### Here is where the search results get displayed

            $count = $q->rowCount();
            if($count >= 1){

            ### Results were found for search term, and the search term is contained somewhere inside
            ### the $this->info property

                echo "<div class=results>
                      <a href=". $this->username .">" . $this->info . "</a></div>";
            }else{
                 $count == 0;
                 echo "<div class=\"alert alert-error\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>No results found for: <strong> " . $_POST['searchbox'] . "</strong></div>";
            }
        }
}

So in the example code above, the property $this->info will contain the search term ($_POST['searchbox']). How can I extract the search term from it?

Thanks very much for any help!

Recommended Answers

All 4 Replies

I suggest you do a str_replace, replacing the searched text with the searched text pre-/appended with the bold tag, in the info field.

Thank you pritaeas, let me make sure I am understanding correctly.

Are you are suggesting that I do something like this:

str_replace($_POST['searchbox'],$this->info,"<strong>" . $_POST['searchbox'] . "</strong>")

Nevermind, got it working thanks to pritaeas for the idea...you are awesome!

Just in case this might help someone else, here is the final code:

I replaced this in the original code:

 if($count >= 1){
### Results were found for search term, and the search term is contained somewhere inside
### the $this->info property
echo "<div class=results>
<a href=". $this->username .">" . $this->info . "</a></div>";
}

with this below:

        if($count >= 1){
            $result = str_replace($_POST['searchbox'], "<strong>" . $_POST['searchbox'] . "</strong>", $this->info);
            echo $result;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.