Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

php - DOMDocument Parse html

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>

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 "

got it

";
        }else{
            echo "

---no ---

";
        }

    }

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

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.

That means you can do

$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...