How to get updated HTML from Symfony DomCrawler component?

I am using the Symfony DomCrawler component. I have successfully retrieving nodes, adding and modifying HTML.

However, I'm not sure how to actually get the HTML at the end. I am trying to get an HTML string after it has been modified using DomCrawler, but I cannot figure out how to do this.

There is no magic __toString() method (and it returns an error when I do print $crawler ). There are no get*() methods, no properties with html as the value. I tried vardump($crawler) , but that does not help.


UPDATE

If i use

 $crawler->first()->ownerDocument->saveHtml() 

it throws an exception about "calling saveHtml () on a non-object", plus a PHP error:

Undefined property: Symfony \ Component \ DomCrawler \ Crawler :: $ ownerDocument

I tried using eq (0) instead of first (), but getting the same error.

However, if I switch to using

 each( function($node, $i) { print $i . " - " . $node; } ) 

then he returns

 0 - <html>...</html> 
+4
source share
2 answers

As it turns out pretty early in the search, I just wanted to indicate that the html() method was added to the scanner in Symfony 2.3

See "Manipulating and Resetting the Crawler" in the Symfony documentation.

+3
source

EDIT: As @dbu pointed out, with Symfony 2.3 you can use the Crawler::html() method.

A crawler is a collection (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:

+6
source

Source: https://habr.com/ru/post/1399762/


All Articles