需要帮助理解如何循环函数


Need help understanding how loop a function

我正在创建一个函数,该函数传递URL并获取页面的内容。如果这个页面包含"下一个>",我想获取它的url,然后继续到页面下一个不再包含下一个的页面。

如何做到这一点?循环一段时间?

check_url("http://site.com");
-> url contains 'next', href is http://site.com/ggkdoe
-> does http://site.com/ggkdoe contain next? if so, hit it again and check if that contains 'next' then get that url etc etc

明白了吗?如何做到这一点?

提前感谢

很可能是这样的:

<?php
$checkNext = false;
$currentURL = "http://site.com";
do {
    $check = check_url($currentURL);
    if ($check !== null) {
       $currentURL = $check;
       $checkNext = true;
    } else {
       $checkNext = false;
    }
} while ($checkNext);

我假设check_url()将返回一个URL(如果可以找到的话),而null将返回另一个URL。do-while-循环确保对初始URL至少进行一次检查,然后只要check_url()能找到另一个URL就再次进行检查。最后,使用$currentURL来做任何你想做的事情。

您可以使用递归性进行完整的链接搜索:

function checkUrl($url) {
    $atLeastOneUrl = true;
    // Check your content
    // Log some data about current Url
    foreach ($urlFound in $urlsFound){
        check_url($urlFound);
        $atLeastOneUrl=true;
    }
return $atLeastOneUrl;
}

但您需要检查链接1-->链接2-->…-->link1循环不会干扰您的搜索;)