1

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";
    }

  }

}

?>

1 Answer 1

3

DOMNode::getNodePath does exactly that, so try echo $node->getNodePath().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.