damian0612 Posted May 4, 2009 Share Posted May 4, 2009 Hi I would like to highlight the keyword in the search result on my page but am not sure how do achieve this. I have been playing with my code for a while without success. Here is the code: <?php $mytime = time() + 3600; //convert to BST $mytimestamp = date('Y-m-d H:i:s', $mytime); $selection = $_GET["day_selected"]; if($_GET["search_all"]!="") {$search = $_GET["search_all"]; $result = mysql_query(" SELECT ItemAddedTime,ItemTitle,ItemLink,ItemSourceURL,ItemDescription FROM feedItems WHERE DATEDIFF('$mytimestamp', `ItemAddedTime`) <= 13 AND ItemTitle LIKE '%$search%' ORDER BY ItemAddedTime DESC", $connection); if(!$result){ die("Database selection failed: " . mysql_error()); } $num_rows = mysql_num_rows($result); If($num_rows > 0){ echo "<table width='100%'><tr><td><b>Search results</b></td></tr>"; echo "<table width='100%'><tr><td><img src='./images/greyline.jpg' width='100%' height='1'></td></tr>"; while($row = mysql_fetch_array($result)){ $linksource=findLinkSource( $row[3] ); $mydate = date('H:i',strtotime($row[0])); echo "<tr><td>$mydate <A href=$row[2] target='blank'>$row[1]</A> $linksource</td></tr>";} echo"</table><br />"; } exit();} ?> [/code So basically I want to highlight any word that matches $_GET["search_all"] in the results. Any help would be appreciated. Thanks Damian Quote Link to comment https://forums.phpfreaks.com/topic/156767-solved-highlight-keyword-in-search-results/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Wrap the matching string with a span class name and use CSS for highlighting. You can use str_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/156767-solved-highlight-keyword-in-search-results/#findComment-825623 Share on other sites More sharing options...
damian0612 Posted May 4, 2009 Author Share Posted May 4, 2009 Wrap the matching string with a span class name and use CSS for highlighting. You can use str_replace(). Thanks for the reply, but can you expand on this. I'm pretty new to php so don't know too much. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/156767-solved-highlight-keyword-in-search-results/#findComment-825761 Share on other sites More sharing options...
premiso Posted May 4, 2009 Share Posted May 4, 2009 $mydate = date('H:i',strtotime($row[0])); $linksource = str_replace($search, "<span>{$search}</span>", $linksource); echo "<tr><td>$mydate <A href=$row[2] target='blank'>$row[1]</A> $linksource</td></tr>";} Should take care of it. Quote Link to comment https://forums.phpfreaks.com/topic/156767-solved-highlight-keyword-in-search-results/#findComment-825766 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Give the span a class name like highlight. Then CSS from that point on. Quote Link to comment https://forums.phpfreaks.com/topic/156767-solved-highlight-keyword-in-search-results/#findComment-825767 Share on other sites More sharing options...
damian0612 Posted May 4, 2009 Author Share Posted May 4, 2009 Thanks to both of you, that works a treat. Quote Link to comment https://forums.phpfreaks.com/topic/156767-solved-highlight-keyword-in-search-results/#findComment-826087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.