asmith Posted July 30, 2008 Share Posted July 30, 2008 hey guys I'm writing a search script. I want to highlight the keyword found in the content. I will do it with str_replace. But i also want to show a few words before it and few words after it. like ... text words bla bla keyword bla bla ... I'm out of ideas how to echo few words before and after the keyword otherwise some contents are so long that will fill all the page. Can someone please give me some ideas? Link to comment https://forums.phpfreaks.com/topic/117392-showing-few-words-before-and-after-the-search-keyword/ Share on other sites More sharing options...
genericnumber1 Posted July 30, 2008 Share Posted July 30, 2008 you could use stristr() along with substr() and/or str_word_count()... personally I'm too tired to think it out for you, but that's where I'd start looking. Link to comment https://forums.phpfreaks.com/topic/117392-showing-few-words-before-and-after-the-search-keyword/#findComment-604039 Share on other sites More sharing options...
asmith Posted July 31, 2008 Author Share Posted July 31, 2008 Thanks for the reply. the stristr or strstr is a good thing. But It do not get the characters before the parametr given IN EARLIER PHP VERSIONS. I'm running php 4.3.3 . hmmm Just gotta find out how to get the words BEFORE a given word... (in my php version ) EDIT : I found it <?php $haystack = 'php-homepage-20071125.png'; $needle = '-'; $result = substr($haystack, 0, strpos($haystack, $needle)); // $result = php ?> Link to comment https://forums.phpfreaks.com/topic/117392-showing-few-words-before-and-after-the-search-keyword/#findComment-604282 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.