php for循环来获取每个URL地址


php for loop to get every URL address

我一直在制作一个PHP for循环来获取每个URL地址。我真的很感谢你事先的帮助。

URL按如下方式递增1:

http://www.4hb.com/letters/index.html
http://www.4hb.com/letters/index1.html
http://www.4hb.com/letters/index2.html
http://www.4hb.com/letters/index3.html

这是我的代码:

for ($i=1;$i<3;$i++) {
    $url="http://www.4hb.com/letters/index".$i.".html";
    include 'class.snoopy.php';
    $s=new snoopy;
    $s->fetch($url);
    $txt=$s->results;
}

也许你忘了}?CCD_ 2也可以从循环中取出。

include 'class.snoopy.php';
$urls = [];
for ($i=1; $i<3; $i++) {
    $url="http://www.4hb.com/letters/index".$i.".html";
    $s = new snoopy();
    $s->fetch($url);
    $urls[] = $s->results;
}
print_r($urls);

对于您的代码,第一个循环只能有$i=1,第二个循环只能只有$i=2,然后循环停止。所以你得到了:

http://www.4hb.com/letters/index1.html
http://www.4hb.com/letters/index2.html

像这样改变你的循环:

for($i=0;$i<4;$i++){
    if ($i==0){
        $url="http://www.4hb.com/letters/index.html";
    }
    else {
        $url="http://www.4hb.com/letters/index".$i.".html";
    }
    include 'class.snoopy.php';
    $s=new snoopy;
    $s->fetch($url);
    $txt=$s->results;
}

我不确定你的问题在哪里,但这解决了循环问题:)