2014-10-10 37 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