DOMDocument解析html [英] DOMDocument Parse html

查看:185
本文介绍了DOMDocument解析html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html页面,其中有许多< tr&t; td> 元素,例如

I have one html page where there are number of <tr><td> elements like

<tr>
<td class="notextElementLabel width100">address:</td>
<td style="width: 100%" colspan="1" class="formFieldelement"><b>12284,CA</b></td>
</tr>

让上面的< tr> 位于第4位,表示在此元素之前还有3个< tr>

let say the above <tr> is at 4th position means before this elements there are 3 more <tr>

现在我想获得地址
的值,所以我正在做

Now I want to get the value of address so I am doing

$doc = new DOMDocument();
    @$doc->loadHTML($this->siteHtmlData);
    $tdElements = $doc->getElementsByTagName("td");
    $i=0;
    foreach ($tdElements as $node) {
        if(trim($node->nodeValue) == 'address:'){
            echo "\n\ngot it\n\n";
        }else{
            echo "\n\n---no ---\n\n";
        }

    }

如何获取 12284,CA。

How can I get the value of "12284,CA". Please guide.

谢谢

推荐答案

在您的情况下,逻辑您的查询后面的代码很简单,可以完全用XPath语法表示:

In your case, the logic behind your query is simple enough that it can be expressed entirely in XPath syntax:

//td[text()="address:"]/following-sibling::td/b/text()

找到任何<$文本等于 address: 的c $ c>< td> 节点,获取以下< ; td> ,进入其中的< b> 并获取其中找到的文本。

This finds any <td> node that has a text equal to "address:", grabs the following <td>, goes into the <b> inside it and gets you the text it finds there.

这意味着您可以

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
echo $xpath->evaluate('string(//td[text()="address:"]/following-sibling::td/b)');

它将立即输出您要查找的结果。

It will immediately output the result you are looking for.

这篇关于DOMDocument解析html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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