Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...