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.

4
  • Why are you using an array when you're only putting content in one key anyway? Commented May 30, 2014 at 7:41
  • print $allUrls[0] before the foreach and I think you figure it out. And if you don't .= will concatenate strings and not add more items in the array. Commented May 30, 2014 at 7:41
  • else $allUrls[0] .= $urls[0]; what do you expect from concatenating? Commented May 30, 2014 at 7:41
  • preg_match_all makes the array. Commented May 30, 2014 at 7:42

4 Answers 4

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>'; 
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
It doesn't display the urls, I only see the error on the screen.
Yes can see the solution here - stackoverflow.com/questions/20017409/…
Just do a print_r($allUrls); and you will get to know why you have that error
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>'; 
}

Comments

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.

2 Comments

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.
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..
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>';
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.