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 6746699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:21:13+00:00 2026-05-26T12:21:13+00:00

When my users search for content on my website I echo $title, $content where

  • 0

When my users search for content on my website I echo $title, $content where like $keyword

The search function implemented works correctly but, because each content field in my database contains quite a few paragraphs of text it makes it hard for users to read when the results are displayed on the page.

I would like the user to search for the keyword entered and then in the results displayed, chop the text some number of letters before and after the keyword and highlight it?

I have read that strpos and strstr should do it. So my question is….

With the following code:

<?php 

foreach ($searchcontent ->result() as $row)
{
echo $title
echo $content
}
?>

How can I highlight the keyword and display a number of characters on each side of the first keyword found??

  • 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-26T12:21:14+00:00Added an answer on May 26, 2026 at 12:21 pm

    You can do something like this:

    In the PHP script

    $keyword = THESEARCHEDKEYWORD;
    $pattern = "/([^a-z])$keyword([^a-z])/i";
    $highlight = '<span class="highlight">' . $keyword . '</span>';
    foreach ($searchcontent->result() as $row)
    {
       $title = preg_replace($pattern, "$1$highlight$2", $title);
       $content = preg_replace($pattern, "$1$highlight$2", $content);
       echo $title;
       echo $content;
    }
    

    And then add this rule in the CSS:

    .highlight
    {
       font-weight: bold;
       background-color: yellow;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On my website users can keyword search and find desired results. (Just a simple
Say you have a website for users to search for hotel rooms for rent.
I have a website where users can search for professional wrestling events in the
I’ve got search lucene set up and running. Everything works perfectly. My website is
I am currently trying to optimize a search function in our website. We are
I'm maintaining an existing website that wants a site search. I implemented the search
I am doing this search engine that should only search my content, but in
I'm developing a website with a custom search function and I want to collect
We're letting users search a database from a single text input and I'm having
My project is similar to: Users search the name of a medicine from 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.