Page 1 of 1

can't get strstr to work

Posted: Mon Feb 14, 2005 9:50 pm
by bimo
I can't understand why I can't find needle in haystack... I am using file_get_contents to get a page of search engine results. Then I search the page to find any anchor tags and put them into the $links array. I am trying to loop through the array and find all of the links that start with "http://" and add them to another array. If it doesn't start with http:// then it's one of the engine's local links.

I'm trying to get it to remove any anchor tag that is a local link but it's not working. Here's the code.

Code: Select all

<form method="get" action="pod_search3.php" name="pod_search">
	<input type="text" name="terms" id="terms" />
	<input type="hidden" name="target" id="target" value="http://www.google.com/search" />
	<input type="submit" value="find mph-cast" />
</form>

Thanks,
<?php
$search_terms = $_GET&#1111;'terms'];
$target_engine = $_GET&#1111;'target'];
$search_terms = str_replace(" ", "+", $search_terms);

$guy = web_search($search_terms, $target_engine);

function web_search($terms, $target) 
&#123;
   
	if($terms) 
	&#123;		
		$query = array();
		
		$query = "$target?hl=en&num=100&li=&q=$terms";
		print($query . "<br>");
		
		$result = file_get_contents($query);
		
		// gets all anchor tags on page
		$pattern = '/(<a .*<\/a>)/i';
		
		preg_match_all($pattern, $result, $links);
		
		$pagelinks = array();
		$num = 0;
		
		// here is the part that's not working
		for($i=0;$i<count($links&#1111;0]);$i++)
		&#123;
		
// what I am trying to say in the next line is, "if http:// is not in the 
// next element, do nothing.  otherwise add it to a new array.

			if(!strstr("http://", $links&#1111;0]&#1111;$i])) ;
			else &#123;
				$pagelinks&#1111;$num] = $links&#1111;0]&#1111;$i];
				print($pagelinks&#1111;0]); //($pagelinks&#1111;$num] . "<br />"); 
				$num++; 
			&#125;
		&#125;
		
		/*foreach($links&#1111;0] as $link)
		&#123;
			//works 
			print("$num - " . $link . "<br />");
			++$num;
		&#125;*/
		
				
	&#125;		
	else print("enter search term");
&#125;
?>
I've tried using,

Code: Select all

continue
after the strstr() line (if(...) continue;) also and it didn't work either. Does anyone see what I have wrong?

...and sorry about the half post. I was trying to make my code more legible by taking out the comments and erased the problem bit.

Thanks

Posted: Mon Feb 14, 2005 10:08 pm
by feyd
and......?

Posted: Mon Feb 14, 2005 10:35 pm
by bimo
sorry about the half post. I think I fixed it above.

Posted: Mon Feb 14, 2005 10:41 pm
by feyd
first problem: you switched the haystack and needle argument places.

after you fix that, 'continue' should work fine.
strstr() wrote:Note: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

Posted: Mon Feb 14, 2005 11:06 pm
by bimo
thanks.