Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the updated HTML from Symfony's DomCrawler component?

Tags:

symfony

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>
like image 618
Dan Blows Avatar asked Mar 05 '12 11:03

Dan Blows


2 Answers

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:

  • DOMElement
  • DOMNode
  • DOMDocument
  • DomCrawler documentation.
  • DomCrawler API documentation
like image 97
Jakub Zalas Avatar answered Oct 22 '22 13:10

Jakub Zalas


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.

like image 30
dbu Avatar answered Oct 22 '22 13:10

dbu