jijodasgupta Posted February 25, 2009 Share Posted February 25, 2009 i hav a search engine runnin... is there a way in php to highlight the searched word in a div using php...????? Link to comment https://forums.phpfreaks.com/topic/146897-highlight-word-in-div/ Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 Yes. str_replace or preg_replace will help to get you started. $string = "highlight the word the in this string"; $string = str_replace("the", "<span class=\"highlight\">the</span>", $string); Link to comment https://forums.phpfreaks.com/topic/146897-highlight-word-in-div/#findComment-771232 Share on other sites More sharing options...
jijodasgupta Posted February 25, 2009 Author Share Posted February 25, 2009 that is wat i had done <?php if(isset($_POST['searchterm'])) { $searchitem=$_POST['searchterm']; //where searchitem contains the word which is searched.. return (ereg_replace($searchitem, "<span class='highlight'>$searchItem</span>", $buffer)); ?> still not workin.... Link to comment https://forums.phpfreaks.com/topic/146897-highlight-word-in-div/#findComment-771251 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 I would suggest against using ereg_replace as it is being depreciated. As far as why it is not working, notice in the replace segment you use "$searchItem" php is case sensitive. It should be all lower case. Link to comment https://forums.phpfreaks.com/topic/146897-highlight-word-in-div/#findComment-771253 Share on other sites More sharing options...
sastro Posted February 25, 2009 Share Posted February 25, 2009 for case insensitive $string = preg_replace('#('.$search.')#si','<span class="highlight">$1</span>',$string); Link to comment https://forums.phpfreaks.com/topic/146897-highlight-word-in-div/#findComment-771256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.