Reputation: 4534
I have a HTML table with class name list
.
I'm using the following query to get the data.
$elements = $xpath->query("//table[@class='list']/tr/td");
$result = $dom_object->saveHTML($elements->item(0));
var_dump($result);
It works fine. Except that it adds the td in the result.
I mean the result look like this
<td>
result data
</td>
Can someone tell me how to remove the td tag from the result data?
Upvotes: 1
Views: 1250
Reputation: 38682
If there is only one text node per cell (ie. no other markup), you can go for
//table[@class='list']/tr/td/text()
which selects all text nodes inside the <td/>
. If there is markup but still only a single text node like in <td><em>foo</em></td>
, you could use
//table[@class='list']/tr/td//text()
If it contains more than one text node, you will receive multiple result nodes which are not grouped by table cell any more.
Upvotes: 0
Reputation: 96149
Maybe you're looking for something like
<?php
$doc = new DOMDocument;
$doc->loadhtml( data() );
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//table[@class='list']/tr/td");
// 1)
$result = (string)$elements->item(0)->nodeValue;
var_dump($result);
// 2)
$frag = $doc->createDocumentFragment();
$node = $elements->item(0)->firstChild;
while( $node ) {
$frag->appendChild( $node->cloneNode(true) );
$node = $node->nextSibling;
}
$result = $doc->saveXML($frag);
var_dump($result);
function data() {
return <<< eoh
<html>
<head><title>...</title></head>
<body>
<table class="list">
<tr><td>result data<br />foo</td></tr>
<tr><td>...</td></tr>
</table>
</body>
</html>
eoh;
}
prints
string(14) "result datafoo"
string(19) "result data<br/>foo"
Upvotes: 1