当我将foreach循环放在for循环范围之外时,它似乎不起作用。
我在这做错了什么:
<?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>';
}
?>
如果我这样做:
<?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>';
}
}
?>
然后它似乎有效。
答案 0 :(得分:2)
改变这个:
$allUrls[0][] = $urls[0];
所以整个代码将是:
$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>';
}
答案 1 :(得分:2)
$allUrls[0] .= $urls[0];
表示将该值附加到数组的第一个元素,这就像添加到值末尾的字符串一样。如果需要向该数组添加新值(子数组位于第0位),请使用此表示法
$allUrls[0][] = $urls[0];
然而,根本不需要那个子阵列。你可以简单地删除这个
if ($i == 0) $allUrls[0] = $urls[0];
else $allUrls[0] .= $urls[0];
并使用
$allUrls[]=$urls[0];
只需像
一样循环播放foreach ( $allUrls as $url ) {
echo $url . '<br>';
}
答案 2 :(得分: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>';
}
试试这个。
答案 3 :(得分:2)
您可以使用以下功能执行此操作:
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();
}
用法:
var_dump(parseMyUrl(1));
var_dump(parseMyUrl(2));
输出:
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)
现在,如果你想在循环中使用它......你可以用一种漂亮而干净的方式来做到这一点:
for ($i = 0; $i <= 1; $i++) {
$urls = parseMyUrl($i);
foreach ($urls as $url) {
echo $url . '<br>';
}
}