I'm using Symfony's DomCrawler component. I have it successfully retrieving nodes, adding and amending HTML.
However, I'm not sure how to actually get the HTML out at the end. I'm trying to get the HTML string after it has been changed by DomCrawler, but I can't find out how to do it.
There's no magic __toString()
method (and it returns an error when I do print $crawler
). There are no get*()
methods, no properties with html as a value. I've tried a vardump($crawler)
but that doesn't help.
UPDATE
If I use
$crawler->first()->ownerDocument->saveHtml()
it throws an exception about "calling the saveHtml() on a non-object", plus a PHP error of:
Undefined property: Symfony\Component\DomCrawler\Crawler::$ownerDocument
I tried using eq(0) instead of first() but get the same error.
However, if I change to using
each( function($node, $i) {
print $i . " - " . $node; }
)
then it returns
0 - <html>...</html>
EDIT: As @dbu pointed out, since Symfony 2.3 it is possible to use the Crawler::html()
method.
Crawler is a set (SplObjectStorage) of DOMElement objects. Knowing that you can use any method and property available in DOMElement, DOMNode and also DOMDocument:
$html = '';
foreach ($crawler as $domElement) {
$html.= $domElement->ownerDocument->saveHTML();
}
echo $html;
Useful links:
As this turns up pretty early when searching, i just wanted to point out that a method html()
was added to the crawler in Symfony 2.3
See "Manipulating and Dumping a Crawler" in the Symfony documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With