Search code examples
phpdomxpath

How get text inside element TD DomXPath


At page there are elements TEXT

I tried make such:

$tables = $xpath->query("//td[@class='enterHeader']");

            foreach($tables as $a){
               print $a->nodeValue." - ".$a->getAttribute("text")."<br/>";
            }

But it did give me result... What is wrong here?


Solution

  • //td[@class='enterHeader']/text()[normalize-space()]

    should get you all text nodes from the cell


    You can cast the results to a string:

    print $xpath->evaluate(
               "string(//td[@class='enterHeader']/text()[normalize-space()])");
    

    Or if you want to get a DOMNodeList and iterate it:

    $tNodes = $xpath->query("//td[@class='enterHeader']/text()[normalize-space()]");
    
    foreach($tNodes as $tNode
      print $tNode->textContent;