PHP DOM遍历HTML节点和子节点 [英] PHP DOM traverse HTML nodes and childnode

查看:106
本文介绍了PHP DOM遍历HTML节点和子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些代码从HTML页面中选出所有的< td> 标签:

I am using some code to pick out all the <td> tags from a HTML page:

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('td') as $node) {
$array_data[ ] = $node->nodeValue;
}

这将数据存储在我的数组中。

This stores the data fine in my array.

正在查看的html数据是:

The html data being looked at is:

<tr>
<td>DATA 1</td>
<td><a href="12345">DATA 2</a></td>
<td>DATA 3</td> 
</tr>

$ array_data 返回:

Array([0])=>DATA 1 [1]=>DATA 2 [2]=> DATA 3)

我想要的输出是从与页面上相关联的> 标签。期望的输出:

My desired output is to get code out of the <a> tag that is associated with the on the page. Desired output:

数组([0])=> DATA 1 [1] => 12345 [2] => DATA 2 [3] =>数据3)

我认为< a> 将被称为子节点,如果这个问题似乎是一个愚蠢的问题,我很开心,很抱歉。

I think <a> would be called child node, I am very new to working with DOM sorry if this seems a stupid question.

我已阅读SO链接:
使用PHP dom获取子元素

我使用这段代码来选出href:

I've used this code to pick out the href:

   foreach ($dom->getElementsByTagName('td') as $node) {
      foreach ($node->getElementsByTagName('a') as $node){
      $link = $node->getAttribute('href');
      echo '<br>';
      echo $link;
      }
      $array_data[ ] = $node->nodeValue;
   }

任何其他阅读材料的帮助或指针都将被大大的欣赏!

谢谢

Any help or pointers for other reading material would be greatly appreicated!
Thanks

推荐答案

您应该检查 td a 孩子。使用 getElementsByTagName()选择锚标签,并使用 长度 属性。如果 td 在小孩中有锚点,请使用 getAttribute() 以获取 href 属性。

You should check td has a child. Select anchor tag using getElementsByTagName() and check the selection has content using length property. If the td has anchor in child, use getAttribute() to get href attribute of it.

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('td') as $node) {
    $nodeAnchor = $node->getElementsByTagName("a");
    if ($nodeAnchor->length)
        $array_data[] = $nodeAnchor->item(0)->getAttribute("href");
    $array_data[] = $node->nodeValue;
}

请参阅演示

这篇关于PHP DOM遍历HTML节点和子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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