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


?>

Perhaps the the the first index produces a negative result?
see:
http://us3.php.net/manual/en/function.array-slice.php
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.