David
David

Reputation: 4518

parsing using DOM Element

Suppose I have some html and I want to parse something from it. In my html I know A, and want to know what is in C . I start getting all td elements, but what to do next ?

I need to check something like " if this td has A as value then check what is written in third td after this. But how can I write it ?

$some_code = ('
....
<tr><td>A</td><td>...</td><td>c</td></tr>
.....
');
$doc->loadHTML($some_code);
$just_td = $doc->getElementsByTagName('td');

foreach ($just_td as $t) {
some code....
}

Upvotes: 2

Views: 376

Answers (1)

Gordon
Gordon

Reputation: 317177

With XPath:

/html/body//tr/td[text()="A"]/following-sibling::td[3]

will find the third sibling of a td element with text content of A that is a child of a tr element anywhere below the html body element.

Upvotes: 5

Related Questions