2012-07-21 112 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