0

I'm trying to keep certain parts of HTML elements inside a dom that was loaded by DomDocument and CURL.

Problem is that when I do xpath query and retireve nodeValue it omits the HTML elements.

Below is the code. Is there a way to retrieve HTML for that particular node?

$location = $xpath->query("//div[@id='location']/label");
echo $location->item(0)->nodeValue."<br>";
Expedito
  • 7,771
  • 5
  • 30
  • 43
Passionate Engineer
  • 10,034
  • 26
  • 96
  • 168

1 Answers1

0
$dom = new DOMDocument();
$dom->loadHTML('<html><div id="location"><label><h1>Hello <b>world</b></h1></label></div></html>');
$xpath = new DOMXPath($dom);
$location = $xpath->query("//div[@id='location']/label/*");


var_dump($dom->saveXML($location->item(0)));

Output:

string(27) "<h1>Hello <b>world</b></h1>"
b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57