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.


$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);
        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(
          // Remove invisible content
          // Add line breaks before and after blocks
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
            "\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));


Perhaps the the the first index produces a negative result?
it will then start from the end of the array.

Just a thought...

i've been playing around with it and it still isn't working

in short .. in php choosing a word from a sentence and echo the word, including the first 5 words before it and after it?

Take a look at the php manual for array_slice(). In the description you should see where you went wrong. It has to do with the arguments of the function.