2014-10-10 33 views
1

我在URL中做了一些操作,如下所示。PHP中传递URL的问题

$paginationPages=array(); 
$productCount=intval($htmlProductPage->find('div.paging span.itemcount',0)->plaintext); 
if($productCount/16>1){ 
    $pagecount=ceil($productCount/16); 

    for($i=2;$i<=$pagecount;$i++){ 
     $urlSplitArray=explode('.',$productUrl); 
     $urlSplitCount=count($urlSplitArray); 
     $urlSplitArray[$urlSplitCount-2].="[".$i."]"; 
     $paginationPages[]= implode('.',$urlSplitArray)."<br>"; 
    } 
#print_r($paginationPages); 

} 

和我得到的的foreach的所有环节进行进一步

foreach($paginationPages as $nextUrl){ 
    #$nextUrl="http://www.100percent.co.nz/kitchen-and-cooking/ovens/freestanding[2].htm"; 
    $htmlProductPage=file_get_html($nextUrl); 
    foreach($htmlProductPage->find("div.Item") as $element){ //error occurs here 
    echo $element->outertext; 
    } 
} 

变量$nextUrl具有价值

http://www.100percent.co.nz/kitchen-and-cooking/ovens/freestanding[2].htm

我不能找到当我p的元素div.Item动态链接链接。但是当我直接在for循环中分配url时,我可以找到相同的元素。为什么发生这种情况?

+0

错误发生在哪里 - 您的代码是否成功使用'file_get_html'获取页面? – 2014-10-10 08:40:40

+0

@ialarmedalien请检查我更新的问题。我已经提到了错误发生的位置 – DharanBro 2014-10-10 08:43:23

+0

链接在每个循环内部进行了评论 – DharanBro 2014-10-10 08:46:18

回答

1

问题发生因本声明:

的print_r的 $paginationPages
$paginationPages[]= implode('.',$urlSplitArray)."<br>"; 

Array 
(
    [0] => http://www.100percent.co.nz/kitchen-and-cooking/ovens/freestanding[2].htm<br> 
) 

您在标记一个<br>元素的网页名称,然后意味着结束无法使用此代码检索页面:

foreach($paginationPages as $nextUrl){ 
    $htmlProductPage=file_get_html($nextUrl); 

I sugge st添加一个检查,file_get_html已成功检索页面,然后再解析页面以擦除内容。

+0

我认为这是我做的一件愚蠢的事情。谢谢。 – DharanBro 2014-10-10 09:01:59

+0

没有probs。有时需要另一个人来追踪这样的错字! – 2014-10-10 09:02:43