需要帮助了解如何循环一个函数

时间:2012-02-28 08:57:47

标签: php function

我正在创建一个传递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

明白了吗?怎么办呢?

提前谢谢

2 个答案:

答案 0 :(得分:0)

很可能是这样的:

<?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(如果找到一个),否则nulldo - while - 循环确保对初始网址至少完成一次检查,然后只要check_url()找到另一个网址,就会再次检查。最后使用$currentURL表示您想要做的事情。

答案 1 :(得分:0)

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

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 - &gt;链接2 - &gt; ... - &gt; link1周期不会干扰您的搜索;)

相关问题