Kate Wintz
Kate Wintz

Reputation: 643

Check content a url for find word

I want check in content a site that did there are some word in it site or no?

Demo: http://codepad.viper-7.com/CQh18s

I tried as:

    $url = "http://www.stackoverflow.com/":
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $contents = curl_exec($ch);
    curl_close($ch);

    $link    = array(
        "Top",
        "Questions",
        "Favorite",
        "Tags",
        "ago",
        "vote",
        "answers"
    );
    $resulet = "";
    foreach ($link as $val) {
        if (strstr($contents, $val)){
            $resulet += '1';
        }else{
            $resulet += '0';
        }
    }
    echo $resulet;
    if ($resulet != 0) {
        echo 'FALSE';
    }else{
        echo 'TRUE';
    }

Upvotes: 0

Views: 292

Answers (2)

The main problem in your code is that you are not letting curl to follow redirections. As @rdlowrey said is just a matter of dumping the $contents var to realize that the page has a new location.

To let curl follow redirections, just add these options before your curl_exec:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);

Besides that, why using $resulet as a counter and not treating it as an integer?

$resulet = 0;
...
$resulet += 1;

Upvotes: 0

user895378
user895378

Reputation:

In situations like this it's helpful to do some initial debugging:

  1. Do a var_dump($contents); first to ensure you're getting the searchable content you expect.
  2. Do a var_dump($resulet); to ensure that your resulting variable is what you expect.

Without further clarification it seems you're trying to create a numeric increment variable in $resulet but doing it with string variables.

The cURL part of your code looks good, so I suspect that if you do this you'll receive the results you expect:

$resulet = 0;
foreach ($link as $val) {
  $resulet += strstr($contents, $val) ? 1 : 0;
}
var_dump($resulet);

If not, you may need to change your strstr to stristr to make the search case-insensitive.

Upvotes: 2

Related Questions