Skip to content

Instantly share code, notes, and snippets.

@gavrya
Forked from jakzal/crawler-edit.php
Created May 5, 2016 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavrya/4fd66c388a38a5846dcee10cbcbb7264 to your computer and use it in GitHub Desktop.
Save gavrya/4fd66c388a38a5846dcee10cbcbb7264 to your computer and use it in GitHub Desktop.
Removing nodes with DomCrawler
<?php
<<<CONFIG
packages:
- "symfony/dom-crawler: ~2.3"
- "symfony/css-selector: ~2.3"
CONFIG;
use Symfony\Component\DomCrawler\Crawler;
$html = <<<HTML
<html>
<div class="content">
<h2 class="gamma">Excerpt</h2>
<p>...content html...</p>
</div>
<div class="content">
<h2 class="gamma">Excerpt</h2>
<p>...more content html...</p>
</div>
</html>
HTML;
$crawler = new Crawler($html, 'http://localhost');
// remove all h2 nodes inside .content
$crawler->filter('html .content h2')->each(function (Crawler $crawler) {
foreach ($crawler as $node) {
$node->parentNode->removeChild($node);
}
});
// output .content nodes with h2 removed
$crawler->filter('html .content')->each(function (Crawler $crawler) {
echo $crawler->html();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment