Reputation: 485
In my continuing saga of xpath and and extracting data I continue to struggle. I need just two values contained in a table cell. I can get to each individually, but I cannot access the other while there. I have cell's like so
<TR>
<TD width="120" align="center" valign="top">
<A href="http://www..yadayada.com"> <!--the href I need to extract-->
<IMG src="http://images.com/items/yada.gif" width="80" height="80" border="1"></A>
<BR>
<B>Random number PT</B><!--the text I need to extract-->
</TD>
I traverse like so:
@$dom = new DOMDocument();
@$dom->loadHTML( $rawPage );
@$xpath = new DOMXPath( $dom );
@$queryResult = $xpath->query( "..../tr/td[contains( b, 'PT' ) ]/b" );
to get to the href link and similary,
@$queryResult = $xpath->query( "..../tr/td[contains( b, 'PT' ) ]/a" );
to get the text I need. And then I extract like so
//for the text in b
foreach ( $queryResult as $result )
{
echo $result->textContent . " text content<br>";
}
and for the link
//for the text in href
foreach ( $queryResult as $result )
{
echo $result->getAttribute( 'href' ) . " href<br>";
}
I do not pull each TD in the table and that's why I match /td[contains( b, 'PT' ) ]
ones that have PT in the . I've read about unions and using /td[contains( b, 'PT' ) ]/*[self::a or self::b
but my for each errors with Invalid argument supplied for foreach()
I've tried using nextSibling and all that too and it just is blank when I echo it. So, how can I get the two values out of my tables?
Upvotes: 0
Views: 453
Reputation: 47331
You can try
//td[contains( b, 'PT' ) ]
And
//td[contains( b, 'PT' ) ]/a
Two queries should work,
Using your existing code
queryResult = $xpath->query( "//td[contains( b, 'PT' ) ]" );
foreach ( $queryResult as $result )
{
echo $result->textContent . " text content<br>";
}
$queryResult = $xpath->query( "//td[contains( b, 'PT' ) ]/a" );
foreach ( $queryResult as $result )
{
echo $result->getAttribute( 'href' ) . " href<br>";
}
Upvotes: 1