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.
Just replace your keyword with a styled
<span>containing the keyword.With
$keyword = "racing"and a text “You know racing? It’s awesome.”,$result->titlewill beThen you simply style the highlight class using CSS, something like
…..