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

  • SEARCH
  • Home
  • 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 7083941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:14:25+00:00 2026-05-28T07:14:25+00:00

How do I highlight keywords in search result? Here’s an example of my code:

  • 0

How do I highlight keywords in search result?

Here’s an example of my code:

<?php
$mysql = new mysqli($host,$username,$password,$database);

$keyword = 'racing';
$query = $mysql->query("SELECT * FROM products WHERE title LIKE '%$keyword%' OR description LIKE '%$keyword%'");

while($result = $query->fetch_object())
{
    echo '<p>';
    echo $result->title.'<br />';
    echo substr($result->description,'0','256');
    echo '</p>';
}
?>

In this code, the keyword is racing, so I would like all the racing in the search result highlighted.

  • 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-28T07:14:26+00:00Added an answer on May 28, 2026 at 7:14 am

    Just replace your keyword with a styled <span> containing the keyword.

    $result->title = preg_replace("/($keyword)/i",'<span class="highlight">$1</span>', $result->title);
    

    With $keyword = "racing" and a text “You know racing? It’s awesome.”, $result->title will be

    You know <span class="highlight">racing</span>? It's awesome.

    Then you simply style the highlight class using CSS, something like

    .highlight { background-color: #ffa; }  
    

    …..

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

Sidebar

Related Questions

i'm using this code to highlight search keywords: function highlightWords($string, $word) { $string =
I have a PHP search script and I want to highlight the keywords that
I'm using this function from here , which is: // highlight search keywords function
I need to highlight, case insensitively, given keywords in a JavaScript string. For example:
I've just read this topic highlight search keywords on hover and actually I use
i've a basic php search form which highlights the keywords using css. i was
I've been trying to highlight a search result in DataTable. First, I loop through
I have a PHP search script that queries a MySQL database and then parses
I am using this class to highlight the search keywords on a piece of
I've seen some websites highlight the search engine keywords you used, to reach the

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.