Jump to content

echo 5 words before 5 words after


MDanz

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.