MDanz Posted March 21, 2010 Share Posted March 21, 2010 I have a keyword "car" in the sentence. one two three four five six seven car eight nine ten eleven twelve thirteen fourteen fifteen. i'd like it to echo three four five six seven car eight nine ten eleven twelve because 5 words before and 5 words after, including car. how do i do this? Quote Link to comment https://forums.phpfreaks.com/topic/196044-echo-5-words-before-5-words-after/ Share on other sites More sharing options...
litebearer Posted March 21, 2010 Share Posted March 21, 2010 Let's see... as I am not good with preg etc, you could explode on 'car', take the 1st part of the resulting array and explode on SPACE, grab the last five elements. do the same with the second part of the origianl array and grab the first five elements. put them together Quote Link to comment https://forums.phpfreaks.com/topic/196044-echo-5-words-before-5-words-after/#findComment-1029730 Share on other sites More sharing options...
Alex Posted March 21, 2010 Share Posted March 21, 2010 You can do it like this (basically): $str = 'one two three four five six seven car eight nine ten eleven twelve thirteen fourteen fifteen'; $words = str_word_count($str, 1); $index = array_search('car', $words); echo implode(' ', array_slice($words, $index - 5, $index + 4)); That's just an example, you should really be doing more checking to make sure that the word you're searching for is found, and that there are 5 words on either side of the word if it is found. Quote Link to comment https://forums.phpfreaks.com/topic/196044-echo-5-words-before-5-words-after/#findComment-1029735 Share on other sites More sharing options...
MDanz Posted March 21, 2010 Author Share Posted March 21, 2010 thx, i've tried to implement it to my code but it's not working exactly. this curls a webpage correctly, the bottom 5 lines of the code is where its not working correctly. It should collect the keyword($search) from the page and the first 5 words before the keyword and after the keyword. i've only got the first part working, it collects the keyword and the first 5 words before it, but not 5 words after the keyword. what have i done wrong? you can change $url and $search(keyword) to see the problem i'm having. <?php $url = $_POST['url']; $search = $_POST['search']; function get_url_contents($url){ $crl = curl_init(); $timeout = 5; curl_setopt ($crl, CURLOPT_URL,$url); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); $ret = curl_exec($crl); curl_close($crl); return $ret; } $sqlone = "INSERT INTO Stacks (`username`,`hyperlink`,`name`,`summary`,`info`,`keywords`,`ip`,`posted`,`adult`) VALUES('$username', '$url', '$name', '$sentence', '$sentence', '$keywords', '$ip', NOW(), '0')"; // $sentence get the 5 words before and 5 words after keyword. // $name is the title of the page or replace with $sentence // $text = get_url_contents($url); function strip_html_tags( $text ) { $text = preg_replace( array( // Remove invisible content '@<head[^>]*?>.*?</head>@siu', '@<style[^>]*?>.*?</style>@siu', '@<script[^>]*?.*?</script>@siu', '@<object[^>]*?.*?</object>@siu', '@<embed[^>]*?.*?</embed>@siu', '@<applet[^>]*?.*?</applet>@siu', '@<noframes[^>]*?.*?</noframes>@siu', '@<noscript[^>]*?.*?</noscript>@siu', '@<noembed[^>]*?.*?</noembed>@siu', // Add line breaks before and after blocks '@</?((address)|(blockquote)|(center)|(del))@iu', '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu', '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu', '@</?((table)|(th)|(td)|(caption))@iu', '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu', '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu', '@</?((frameset)|(frame)|(iframe))@iu', ), array( ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", ), $text ); return strip_tags( $text ); } $test = strip_html_tags( $text ); $str = "$test"; $words = str_word_count($str,1); $index = array_search($search, $words); echo implode(' ', array_slice($words, $index - 5, $index + 5)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/196044-echo-5-words-before-5-words-after/#findComment-1029777 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.