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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:40:29+00:00 2026-06-16T00:40:29+00:00

Wondering if someone could help give me a push in the right direction, I

  • 0

Wondering if someone could help give me a push in the right direction, I am building a search function (php and mysql) which will display search results and highlights keywords that the user has searched for. at the moment I grab the search criteria that the user has entered and query that against the database which works fine to get the desired results. the problem I have is

$highlight = preg_replace("/".$_GET['criteria']."/", "<span class='highlight'>".$_GET['criteria']."</span>", $_row['name']); 

This will only highlight a phrase and not individual keywords. so for example if the document was called “Hello world” and the user typed this exactly it would highlight no problem however if the user typed “world hello” it will not highlight anything. I thought it would be a good idea to take the search criteria and use explode and check each word individually but this seems to fail as well. here is my query and how I am displaying results

    $sql = "SELECT *
                FROM uploaded_documents
                WHERE dept_cat = 'procedures'
                AND cat =:cat 
                AND keywords REGEXP :term ";
    $result->execute(array(':cat' => $_GET['category'],':term' => $_GET['criteria']));

 //display results
 while($row = $stmt->fetch()){
    $explode_criteria = explode(" ",$_GET['criteria']);             
    foreach($explode_criteria as $key){                             
        $highlight = preg_replace("/".$key."/", "<span class='highlight'>".$key."</span>", $row['name']); 

            echo '<td><a target="_blank" href="'.$row['url'].'">'.$highlight.'</a></td>';   
                    echo '<td>'.$row['version'].'</td>';
                    echo '<td>'.$row['cat'].'</td>';
                    echo '<td>'.$row['author'].'</td>'; 

                    echo '<td>'.$row['added'].'</td>';  
                    echo '<td>'.$row['auth_dept'].'</td>';  

                    echo '<td>';
    } 
}

For the sake of length I have omitted code here and tried to keep it minimal, I have been trying to base my work on the following post

highlighting search results in php/mysql

I think my first problem is the foreach loop in the while loop duplicating results but I cant think of a way around it.

Thanks in advance

  • 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-06-16T00:40:29+00:00Added an answer on June 16, 2026 at 12:40 am

    In this block of code:

    //display results
    while ($row = $stmt->fetch())
    {
        $explode_criteria = explode(" ", $_GET['criteria']);
        foreach ($explode_criteria as $key)
        {
            $highlight = preg_replace("/" . $key . "/", "<span class='highlight'>" . $key . "</span>", $row['name']);
    
            echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
            echo '<td>' . $row['version'] . '</td>';
            echo '<td>' . $row['cat'] . '</td>';
            echo '<td>' . $row['author'] . '</td>';
    
            echo '<td>' . $row['added'] . '</td>';
            echo '<td>' . $row['auth_dept'] . '</td>';
    
            echo '<td>';
        }
    }
    

    The loop is constantly referring to $row['name'], so the replacement is done, but the next time the loop happens it is replacing the next word on the original unmodified $row['name']

    I think this should help you:

    //display results
    while ($row = $stmt->fetch())
    {
        $explode_criteria = explode(" ", $_GET['criteria']);
        $highlight = $row['name']; // capture $row['name'] here
        foreach ($explode_criteria as $key)
        {
            // escape the user input
            $key2 = preg_quote($key, '/');
            // keep affecting $highlight
            $highlight = preg_replace("/" . $key2 . "/", "<span class='highlight'>" . $key . "</span>", $highlight);
    
            echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
            echo '<td>' . $row['version'] . '</td>';
            echo '<td>' . $row['cat'] . '</td>';
            echo '<td>' . $row['author'] . '</td>';
    
            echo '<td>' . $row['added'] . '</td>';
            echo '<td>' . $row['auth_dept'] . '</td>';
    
            echo '<td>';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone could help me in the right direction. How could
Just wondering if someone could point me in the right direction of how to
Was wondering if someone could give me some direction on this. I've spent a
I was wondering if someone could help me out. Im building a forum into
Wondering if someone could help me. I have next to no knowledge with Ajax,
Just wondering if someone could help me with a very simple SQL query. I
I was wondering if someone could help me with this. I have defined my
Python neophyte here. I was wondering if someone could help with the KeyError I
Hey guys I was wondering if someone could provide a little help. I've been
Im wondering if someone could help me out with a little issue im having.

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.