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.
There’s a lot of room for improvement, but here’s an approach:
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:
with:
Some improvemets you could make to this function: