使用domDocument获取src元素 [英] getting src element using domDocument

查看:173
本文介绍了使用domDocument获取src元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用domDocument。我已经接近了,但最后一点需要帮助

I am using domDocument. I am close but need help for the last little bit

我下面有一个html片段。
有很多行。

I have this html just a snippet below. There are a number of rows. I am trying to get the href.

到目前为止,我正在执行以下操作:
我可以使表,tr和td正常,但不确定

so far i am doing the following: I can get the table, tr, and td ok , but not sure what to do from there.

感谢您的帮助

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>


推荐答案

让我为您介绍xpath(查询)的概念DomDocuments的语言:

Let me introduce you the concept of xpath, a query language for DomDocuments:

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

读取为:取具有类属性tableStyle02的表标签,然后从子标签中获取href属性。

Reads as: Take the table tag with class attribute tableStyle02 and then the href attribute from within the a child tag.

或者就像您在 tr td 元素中使用foreach一样好:

Or as you had the foreach for tr and td elements as well:

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

现在,该标记是td标记的直接子代,而td标记是tr标记的直接子代这是table标记的直接子代。如您所见,使用xpath可以比使用PHP代码编写所有内容更容易地制定元素的路径。

Now in that path, the a tag is a direct children of the td tag which is a direct children of the tr tag which is a direct children of the table tag. As you can see, with xpath it is much easier to formulate the path to the element than writing everything in PHP code.

Apropos PHP代码,在PHP中,此可以如下所示:

Apropos PHP code, in PHP this can look like:

$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

The variable $href then contains the string: bin.php?cid=703&size=0.

此示例使用字符串( string(...)),因此-> evaluate 返回一个字符串,它是从第一个找到的属性节点创建的。相反,您也可以返回一个节点列表:

This example is with a string (string(...)), so ->evaluate returns a string, which is created from the first found attribute node. Instead you can return a nodelist as well:

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

现在 $ hrefs 包含通常的 DOMNodeList ,此处包含所有href属性节点:

Now $hrefs contains the usual DOMNodeList, here it contains all the href attribute nodes:

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

请注意,如果仅使用一个斜杠 / 分隔标记,它们必须是直接子代。用两个斜杠 // 可以是后代(子代或子代(...的子代))。

Take care that if you use only one slash / to separate tags, that they need to be direct children. With two slashes // it can be a descendant (child or child of child (of child (of ...))).

这篇关于使用domDocument获取src元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆