I’m using the DOMCrawler to select HTML based on a CSS selector, like so;
$selector = '#content';
$html = $my_fetched_html;
$crawler = new Crawler($html);
$crawler = $crawler->filter($selector);
This is all good and if I iterate through the $crawler elements, it confirms that the selector worked, ie, I get only a div and not the full html.
But, when it comes to saving out the HTML I have problems, because doing something like this;
$html = '';
foreach ($crawler as $domElement) {
$html.= $domElement->ownerDocument->saveHTML();
}
echo $html;
(As demonstrated in this question)
I get the full structure again and I’m assuming its because $ownerDocument is still the original (unselected) $crawler document.
So, what I need is to be able to instantiate a new DomDocument (perhaps?) add my Element to it and then saveHTML() from there. I think?
Pseudo code;
$doc = new DomDocument
foreach ($crawler as $domElement) {
$doc->addChild($domElement);
}
$new_html = $doc->saveHTML();
I’d love any help, its starting to do my head in.
You need to pass the child for which you want the html to saveHTML(). i.e. do this: