PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have an HTML string/page stored in variable $html. I would like to get part of the html of this page which has a body with class=foo and multiple divs including div[id=bar]. So I am trying to get :
I am first getting the node I want with xpath ( you could probably use also use getElementById, getElementsByTagName ) and them importing this into a new domdocument node
$dom = new DomDocument();
@$dom->loadHTML($html);
// CREATE XPATH OBJECT -
$xpath = new DOMXPath($dom);
// MAKES A DOMNODE NOT A DOMNODELIST, because you have to import a domnode!
$body=$xpath->evaluate("//div[@id='body']")->item(0) ;
$subtree=new DOMDocument();
$node2=$subtree->importNode($body, TRUE); // true imports node's subtree not just targeted node
$subtree->appendChild($node2); // / And then append it to the "<root>" node
print($subtree->saveHTML());