...

View Full Version : strpos() Isn't Returning Accurate Results???



Sussex_Chris
02-27-2010, 03:41 AM
I have created a script that uses CURL to retrieve a webpage where it searches to see if a link exists.

When this search takes place it also completely rips the page at the moment so I could find the problem as I knew that something was up with it.

I imported 1000 URL's to rip and scan for a link and then ran 3 scans on the full list.

Scan 1 returned 317/1000 ok.

Scan 2 returned 312/1000 ok.

Scan 3 returned 319/1000 ok.

I have looked at the page rips to see the differences in the URL's that it was saying did not have the link in place and they were there. In exactly the same format as the previous searches.

So in 3 searches of the same webpage only once did it return ok on the strpos match when all 3 times it was scanning the same page.

Is this a problem with CURL not grabbing the page fast enough?

Inigoesdr
02-27-2010, 05:02 AM
How about some example code so we can see/try what you're talking about?

Sussex_Chris
02-27-2010, 05:09 AM
This is the script I am using, with all DB side of it removed for easy reading:



<?php
function Return_Content_From_URL($url){
$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
$html= curl_exec($ch);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($html));
return $content;
}

$url = "http://urltosearch.com/";
$result = get_search_results($url);

$pos = strpos($result, "keyword");
if($pos==false){echo "Keyword Not Found In URL: $url<br />";}
?>

MattF
02-27-2010, 05:32 AM
From the strpos page:



This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.


http://uk2.php.net/manual/en/function.strpos.php



if (strpos($result, "keyword") !== false)
{
echo "Keyword Not Found In URL: $url<br />";
}

Sussex_Chris
02-27-2010, 06:26 AM
From the strpos page:



http://uk2.php.net/manual/en/function.strpos.php



if (strpos($result, "keyword") !== false)
{
echo "Keyword Not Found In URL: $url<br />";
}

This seems to be returning true whether the value be there or not. I think this works ok:

if (strpos($result, "keyword") === false)

MattF
02-27-2010, 06:42 AM
if (strpos($result, "keyword") === false)


Yup. That's the one you should be using. A slight error on my part there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum