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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:48:24+00:00 2026-06-18T18:48:24+00:00

First i like to thank for helping me create a really nice search result

  • 0

First i like to thank for helping me create a really nice search result query.
I hope you friends can help me improve it.

here is the query

$searchresult = $mysql->query("SELECT * FROM pages WHERE PageContent LIKE '%$searchTerm%'");

if($searchresult->num_rows > 0) {

    while (($row = $searchresult->fetch_assoc()) !== null)
    {
        echo '<h3>' . $row["PageTitle"] . '</h1>';

        $position = strpos($row["PageContent"], $search);
        $snippet = substr($row["PageContent"], $position - 200, $position + 200);

        echo $snippet . '<hr><br />';  
    }

} else {
     echo "<p>Sorry, but we can not find an entry to match your query</p><br><br>"; 
}

What I like to do is to make the snippet trim in such a way that it do not break any word so that the sentence is readable …. and if possible to make the search term appear in bold.
Dear friends i need your help in doing so.
Thank you all 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-18T18:48:26+00:00Added an answer on June 18, 2026 at 6:48 pm

    There’s a lot of room for improvement, but here’s an approach:

    <?php
    
    echo trimsnippet("Some really, really, really reaaally long text I don't really care about, at all.", "text", 30) . "\n";
    
    
    function trimsnippet($text, $query, $max_length){
      $position = strpos($text, $query); 
      $snippet = substr($text, max($position - $max_length - 1, 0), strlen($query) + $max_length*2 + 1);
      echo "<$snippet>\n";
      preg_match("/[^\w](?P<pre>.*)".$query."(?P<post>.*)[^\w]/", $snippet, $extracted); 
      return $extracted["pre"]."<strong>".$query."</strong>".$extracted["post"];
    }
    

    Output:

    really, really reaaally long text I don’t really care about,

    How to use it:

    Disregard the first line (echo…), it’s just a demo.
    Place the function anywhere in the php that uses it, and then replace your lines:

        $position = strpos($row["PageContent"], $search);
        $snippet = substr($row["PageContent"], $position - 200, $position + 200)
    

    with:

        $snippet = trimsnippet($row["PageContent"], $search, 200);
    

    Some improvemets you could make to this function:

    • check that $query really appears in the $text
    • add an ellipsis (…) to the beginning or the end of the result
    • control what happens when the $query is near the beginning and the end (and avoid adding the ellipsis in that case)
    • sanitize the $query (and probably the $text also) so it cannot contain “regex syntax” that could mess with your regex pattern.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right first of I would like to thank everyone for helping me out on
Hoping you can help. I have three tables and would like to create a
You can validate a model object with EF 5 Code-First like that: var validationResult
I remember seen some code xaml where can get the first element (like an
First off, let me thank the SO community for helping me so many times
first of all, I'm very new to Powershell, and I would like to thank
First off I would like to say that I think that my question may
I would first like to admit that I am an extremely novice developer, so
Come across what looks at first sight like an MT-issue, but I'm trying to
I have text file with something like first line line nr 2 line three

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.