0

When i put foreach loop outside the for loops scope then it doesn't seem to work.
What am I doing wrong here:

<?php
$allUrls = array();

for( $i = 0; $i <= 1; $i++ ) {

    $html = file_get_contents("http://www.keurmerk.info/Leden_Partners?s=&c=0&Page=".$i."");
    $pattern = "/(http(s)?:\/\/)?(w{3}\.)(\w+\.)([a-zA-Z]{2,6})(\/\w*)?/";
    preg_match_all( $pattern, $html, $urls );

    if ($i == 0) $allUrls[0] = $urls[0];
    else $allUrls[0] .= $urls[0];

}           

foreach ( $allUrls[0] as $url ) {
    echo $url . '<br>'; 
}
?>

If I do it like this:

<?php
for( $i = 0; $i <= 1; $i++ ) {

    $html = file_get_contents("http://www.keurmerk.info/Leden_Partners?s=&c=0&Page=".$i."");
    $pattern = "/(http(s)?:\/\/)?(w{3}\.)(\w+\.)([a-zA-Z]{2,6})(\/\w*)?/";
    preg_match_all( $pattern, $html, $urls );
    $allUrls[0] = $urls[0];
    foreach ( $allUrls[0] as $url ) {
        echo $url . '<br>';
    }
}
?>

then it seems to work.

Legionar
  • 7,472
  • 2
  • 41
  • 70
user3639768
  • 121
  • 1
  • 7

4 Answers4

2

Change this:

$allUrls[0][] = $urls[0];

So the whole code will be:

$allUrls = array();

for( $i = 0; $i <= 1; $i++ ) {

    $html = file_get_contents("http://www.keurmerk.info/Leden_Partners?s=&c=0&Page=".$i."");
    $pattern = "/(http(s)?:\/\/)?(w{3}\.)(\w+\.)([a-zA-Z]{2,6})(\/\w*)?/";
    preg_match_all( $pattern, $html, $urls );

    $allUrls[$i] = $urls[0];

}           

foreach ( $allUrls as &$url ) {
    echo $url . '<br>'; 
}
Legionar
  • 7,472
  • 2
  • 41
  • 70
2
$allUrls[0] .= $urls[0];

Means append that value to the first element of your array, and that is just like a string being added to the end of your value. If you need to add a new value to that array (the sub array at the 0th position), use this notation

$allUrls[0][] = $urls[0];

That sub array however, is not really needed at all. You can simply remove this

if ($i == 0) $allUrls[0] = $urls[0];
else $allUrls[0] .= $urls[0];

And use

 $allUrls[]=$urls[0];

And simply loop over it like

foreach ( $allUrls as $url ) {
    echo $url . '<br>'; 
}
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
2
$allUrls = array();
 for( $i = 0; $i <= 1; $i++ ) {

$html = file_get_contents("http://www.keurmerk.info/Leden_Partners?s=&c=0&Page=".$i."");
$pattern = "/(http(s)?:\/\/)?(w{3}\.)(\w+\.)([a-zA-Z]{2,6})(\/\w*)?/";
preg_match_all( $pattern, $html, $urls );

$allUrls[$i] = $urls[0]


}           

foreach ( $allUrls as $url ) {
    echo $url . '<br>'; 
}

try this.

Raymond Cheng
  • 2,425
  • 21
  • 34
  • I'm getting the following notice: `Notice: Array to string conversion in line 102` it's this line `foreach ( $allUrls as $url ) {` which is giving the error. – user3639768 May 30 '14 at 07:48
  • use var_dump or print_r instead of echo in the foreach loop..see if it is array or string...if it is string that means in for loop $urls[0] is array..var_dump it step by step..i didn't test it..so.. – Raymond Cheng May 30 '14 at 07:58
2

You can do it like this, using a function:

function parseMyUrl($pageNo) {
    $html = file_get_contents("http://www.keurmerk.info/Leden_Partners?s=&c=0&Page=". $pageNo);
    $pattern = "/(http(s)?:\/\/)?(w{3}\.)(\w+\.)([a-zA-Z]{2,6})(\/\w*)?/";
    preg_match_all($pattern, $html, $urls);
    if ($urls[0])
        return $urls[0];
    else
        return array();
}

Usage:

var_dump(parseMyUrl(1));
var_dump(parseMyUrl(2));

Outputs:

array (size=21)
  0 => string 'http://www.bikinisonline.eu' (length=27)
  1 => string 'http://www.handpoppen.net' (length=25)
  2 => string 'http://www.123kinderfietsen.nl' (length=30)
  3 => string 'http://www.123ledspots.nl' (length=25)
  4 => string 'http://www.123mijngordijn.nl' (length=28)
  5 => string 'http://www.123soatest.nl' (length=24)
  6 => string 'http://www.123sportfietsen.nl' (length=29)
  7 => string 'http://www.123superfoods.nl' (length=27)
  8 => string 'http://www.123telefoon.nl' (length=25)
  9 => string 'http://www.123tuinleds.nl' (length=25)
  10 => string 'http://www.123voetmassage.nl' (length=28)
  11 => string 'http://www.12cook.com' (length=21)
  12 => string 'http://www.1gameshop.be' (length=23)
  13 => string 'http://www.24parfums.nl' (length=23)
  14 => string 'http://www.2wielerwinkel.nl' (length=27)
  15 => string 'http://www.4activekidz.nl' (length=25)
  16 => string 'http://www.4kidsathome.nl' (length=25)
  17 => string 'http://www.4kidsnederland.nl' (length=28)
  18 => string 'http://www.4moregames.nl' (length=24)
  19 => string 'http://www.4sporters.nl' (length=23)
  20 => string 'https://www.extremetracking.com' (length=31)

array (size=19)
  0 => string 'http://www.goedkopesneeuwkettingen.nl' (length=37)
  1 => string 'http://www.bikinisonline.eu' (length=27)
  2 => string 'http://www.villatotaal.nl' (length=25)
  3 => string 'http://www.4yoursport.nl' (length=24)
  4 => string 'http://www.4youwear.nl' (length=22)
  5 => string 'http://www.6566.eu' (length=18)
  6 => string 'http://www.aadenwijn.nl' (length=23)
  7 => string 'http://www.aagifts.nl' (length=21)
  8 => string 'http://www.aanhangershop.nl' (length=27)
  9 => string 'http://www.aanhangwagendirect.nl' (length=32)
  10 => string 'http://www.aannemerskorting.nl' (length=30)
  11 => string 'http://www.abcoparts.nl' (length=23)
  12 => string 'http://www.aboutshoes.nl' (length=24)
  13 => string 'http://www.accudienst.nl/' (length=25)
  14 => string 'http://www.acculaptop.com' (length=25)
  15 => string 'http://www.accuserviceholland.nl' (length=32)
  16 => string 'http://www.accushop.nl' (length=22)
  17 => string 'http://www.accuweb.nl' (length=21)
  18 => string 'https://www.extremetracking.com' (length=31)

Now if you want to use it in a loop.... you can do this in a nice and clean way:

for ($i = 0; $i <= 1; $i++) {
    $urls = parseMyUrl($i);
    foreach ($urls as $url) {
        echo $url . '<br>';
    }
}
Latheesan
  • 23,247
  • 32
  • 107
  • 201