Unexpected results from loop

I want to ignore all results in $list1, for some reason it’s skipping the entire code. When I put in a single
if(strpos($href, $lista) !== false) {
continue; }
It works, but my loop is not, can anyone explain this to an idiot?
$linkList = $parser->getLinks();

foreach($linkList as $link){
	$href = $link->getAttribute("href");
	
	$list1 = array("#", "google.", "facebook.", "twitter.", "https://t.co", "bbpress.", "wordpress.", "amazon.", "buddypress.", "youtube.");
	
	foreach($list1 as $lista)	{
			if(strpos($href, $lista) !== false) {
		continue; 
		}
	
	}	
			
 		$href = createLink ($href, $url);
		
		if(!in_array($href, $alreadyCrawled)) {	
		$alreadyCrawled[] = $href;
		$crawling[] = $href;
		}

And how should we know what your input is? Did you try to debug this by yourself using var_dump on all variables and function outputs? This is easy to mockup and bring into a function, i would redommend using array_filter. But at least i don’t see any use for that loop, it does plain nothing with the resulting condition.

<?php

class Link {
    public function __construct($link){
        $this->link = $link;
    }
    public function getAttribute($attribute){
        return $this->link;
    }
};

$linkList = [
    new Link('yes #'),
    new Link('yes google.com'),   
    new Link('facebook.net yes'),    
    new Link('no'),    
    new Link('no'),
];

foreach($linkList as $link){
	$href = $link->getAttribute("href");
	
	$list1 = array("#", "google.", "facebook.", "twitter.", "https://t.co", "bbpress.", "wordpress.", "amazon.", "buddypress.", "youtube.");
	
	foreach($list1 as $lista)	{
		var_dump(strpos($href, $lista));
	    echo "\n";
	}
}

I am busy developing a crawler and everything acts as expected till I change the long list of else if statements to an array with a for each statement. I figure it will be easier to create an array of urls to ignore from a database, than creating a new else if statement for every url I don’t want to crawl.

The only thing not working is the loop, the input is a list of URL’s. If I individually post each variable in the array in it’s own if statement, I get the expected result. I am trying to make a long list of if else if statements shorter.

if(strpos($href, "#") !== false) {
			continue; 
			}
else if(strpos($href, "google.") !== false) {
			continue; 
			}
else if(strpos($href, "facebook.") !== false) {
			continue; 
			}

etc.

The above statements work, but the moment I set up the loop, it fails. The statement below is not working, and I am buggered if I can figure out why.

$list1 = array("#", "google.", "facebook.", "twitter.", "https://t.co", "bbpress.", "wordpress.", "amazon.", "buddypress.", "youtube.");
		
		foreach($list1 as $lista)	{
				if(strpos($href, $lista) !== false) {
			continue; 
			}
		
		}

Continue continues to the next iteration of the loop it’s in. Because you’re in a second loop inside the main one, your new foreach through the array, that’s the loop that you continue around, not your outer one. As your code stands, all you do is run through the inner loop doing nothing different whether or not any array element is found in the href.

As per the documentation, you can specify how many levels of loop you skip around, so in your code here, you could use continue 2 to do what I think you want. But it doesn’t seem very nice - if you refactor the code at any point you’d have to alter that number.

Off the top of my head, I’d probably set a flag to indicate whether the value was found in my inner loop, and after that loop is closed, check it and continue at that point. But there must be some nicer way.

$found = false;
foreach($list1 as $lista)	{
  if(strpos($href, $lista) !== false) {
    $found = true; 
    break;
    }
  }	
			
if ($found == true) continue;

Thanks, I will try this.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.