2012-07-21 109 views
1

我正在使用domDocument。我很接近,但最後一點點需要幫助使用domDocument獲取src元素

我有這個HTML只是一個片段下面。 有許多行。我正在嘗試獲取href。

到目前爲止,我正在做以下幾點: 我可以得到表格,tr和td ok,但不知道該怎麼做。

感謝所有幫助

foreach ($dom->getElementsByTagName('table') as $tableitem) { 
    if ($tableitem->getAttribute('class') == 'tableStyle02'){ 
     $rows = $tableitem->getElementsByTagName('tr'); 
     foreach ($rows as $row){ 
      $cols = $row->getElementsByTagName('td'); 

      $hrefs = $cols->item(0)->getElementsByTagName('a'); 
     }  
    } 
} 

HTML片段:

<table width="100%" border="0" cellspacing="0" cellpadding="2" class="tableStyle02"> 
    <tr> 
     <td><span class="Name"><a href="bin.php?cid=703&size=0"> 
       <strong>Conference Facility</strong></a></span></td> 
     <td align="center" nowrap>0.00</td> 
     <td align="center">&nbsp;0&nbsp;</td> 
     <td align="center">&nbsp;&nbsp;</td> 
     <td align="center">&nbsp;0&nbsp;</td> 
     <td align="center">&nbsp;0&nbsp;</td> 
     <td align="center">&nbsp;0 - 0 &nbsp;</td> 
     <td align="center">&nbsp;Wired Internet,&nbsp;&nbsp;&nbsp;</td> 
     <td align="center">&nbsp;&nbsp;</td> 
    </tr> 

回答

3

讓我介紹你的XPath的理念,爲DomDocuments查詢語言:

//table[@class="tableStyle02"]//a/@href 

讀取爲:帶有類屬性tableStyle02的table標籤,然後是a中的href屬性兒童標籤。

或者你有在foreach爲trtd元素以及:

//table[@class="tableStyle02"]/tr/td/a/@href 

現在,在這條道路,在一個標籤是td標籤是tr標籤的直接孩子的直接孩子這是桌子標籤的直接子項。正如您所看到的,使用xpath,比在PHP代碼中編寫所有內容更容易制定元素的路徑。

中肯的PHP代碼,在PHP中,這可以樣子:

$doc = new DOMDocument(); 
$doc->loadHTML($html); 
$xp = new DOMXPath($doc); 
$href = $xp->evaluate('string(//table[@class="tableStyle02"]//a/@href)'); 

變量$href則包含字符串:bin.php?cid=703&size=0


這個例子是用一個字符串(string(...)),所以->evaluate返回一個字符串,它是從第一個被發現的屬性節點創建的。相反,你可以返回一個節點列表,以及:

$hrefs = $xp->query('//table[@class="tableStyle02"]/tr/td/span/a/@href'); 
#    ^^^^^          ^^^^ 

現在$hrefs包含通常DOMNodeList,這裏包含了所有的href屬性節點:

echo $hrefs->item(0)->nodeValue; # bin.php?cid=703&size=0 

小心,如果你只使用一個斜槓/到單獨的標籤,他們需要成爲直接的孩子。用兩個斜槓//它可以是一個後裔(小孩的孩子或小孩(...)))。

+0

這是偉大的,所有新的。所以我打了一些,我還有一個問題。我的表有很多行,並有多個hrefs。我做了$ href = $ xp-> evaluate('string(// table [@ class =「tableStyle02」]/tr/td/a/@ href)');但只拿到第一名。我如何獲得全部? – randy 2012-07-21 16:32:17

+0

當然,我編輯了這個變體的答案。您可以像之前一樣''foreach'over'$ hrefs'。所以這兩種方法一起工作得很好 – hakre 2012-07-21 16:41:43

1

你應該能夠對個人一個DOMElement實例使用getAttribute(),(就像你使用它的例子中的第二行):

foreach ($hrefs as $a_node) { 
    if ($a_node->hasAttribute('href')) { 
     print $a_node->getAttribute('href'); 
    } 
} 
1

您不必沿着DOM層次使用getElementsByTagName

foreach ($dom->getElementsByTagName('table') as $tableitem) { 
    if ($tableitem->getAttribute('class') == 'tableStyle02'){ 
     $links = $tableitem->getElementsByTagName("a"); 
    } 
} 

$links在這一點現在是一個DOMNodeList,這樣你就可以遍歷它:

foreach ($dom->getElementsByTagName('table') as $tableitem) { 
    if ($tableitem->getAttribute('class') == 'tableStyle02'){ 
     $links = $tableitem->getElementsByTagName("a"); 
     $hrefs = array(); 
     foreach ($links as $link) { 
      $hrefs[] = $link->getAttribute("href"); 
     } 
    } 
} 
// Do things with $hrefs