Reputation: 4801
I want to find/create the xpath of all elements found after my query runs.
I tried, but I got stuck.
This code searches through all elements/nodes for any values that contain the letter 't'. If 't' is found , I would like to know the xpath of that element:
<?php
$dom = new DOMDocument();
@$dom->loadHTMLFile('http://www.linkbook.co/');
//use DOMXpath to navigate the html with the DOM
$dom_xpath = new DOMXpath($dom);
$elements = $dom_xpath->query("//*[text()[contains(., 't')]]");
var_dump($elements);
if (!is_null($elements)) {
foreach ($elements as $element) {
var_dump($element);
echo "\n[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
Upvotes: 1
Views: 1366
Reputation: 437376
DOMNode::getNodePath
does exactly that, so try echo $node->getNodePath()
.
Upvotes: 3