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...????? Quote 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); Quote 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.... Quote 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. Quote 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); Quote Link to comment https://forums.phpfreaks.com/topic/146897-highlight-word-in-div/#findComment-771256 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.