Goutte - dom crawler - remove node

I have html on my website ( http://testsite.com/test.php ):

<div class="first">
  <div class="second">
     <a href="/test.php">click</a>
     <span>back</span>
  </div>
</div>
<div class="first">
  <div class="second">
     <a href="/test.php">click</a>
     <span>back</span>
  </div>
</div>

I would like to receive:

<div class="first">
  <div class="second">
     <a href="/test.php">click</a>
  </div>
</div>
<div class="first">
  <div class="second">
     <a href="/test.php">click</a>
  </div>
</div>

So I would like to remove the span. I am using Goutte in Symfony2 based on http://symfony.com/doc/current/components/dom_crawler.html :

    $client = new Client();
    $crawler = $client->request('GET', 'http://testsite.com/test.php');

    $crawler->filter('.first .second')->each(function ($node) {
        //??????
    });
+4
source share
2 answers

As described in the docs :

The DomCrawler component simplifies DOM navigation for HTML and XML documents.

and:

Although possible, the DomCrawler component is not designed to handle DOM or re-dump HTML / XML.

DomCrawler DOM, .

...

PHP , Crawler - DOMNode s, DOM :

// will remove all span nodes inside .second nodes
$crawler->filter('html .content h2')->each(function (Crawler $crawler) {
    foreach ($crawler as $node) {
        $node->parentNode->removeChild($node);
    }
});

: https://gist.github.com/jakzal/8dd52d3df9a49c1e5922

+3

To remove a node the anonymous function must return false.

false , $ node .

0

All Articles