I have a PHP search script and I want to highlight the keywords that the user has searched with tags. How can I do this?
My code is:
if(!empty($_GET['q'])){
$keywords=explode(' ',$_GET['q']);
foreach($keywords as $query){
$query=mysql_real_escape_string($query);
$likes[]="keywords LIKE '%{$query}%'";
}
$searchResult=mysql_query("select * from questions where ".implode('or ',$likes)."limit 1");
while($row=mysql_fetch_assoc($searchResult)){
$results="<div class='webresult'>{$row['result']}</div>";
}
}
If
$row['result']does not contain html, you can do a string replace: replace all keywords in the result with<span class="highlight">keyword</span>A simple example: