Skip to content

Instantly share code, notes, and snippets.

@fredysan
Last active February 4, 2020 23:33
Show Gist options
  • Save fredysan/87bf4616e82811ba711f6c3e17301419 to your computer and use it in GitHub Desktop.
Save fredysan/87bf4616e82811ba711f6c3e17301419 to your computer and use it in GitHub Desktop.
Remove image tags conditionally based on src content
<?php
use Symfony\Component\DomCrawler\Crawler;
class CustomCrawler {
protected function removeExtLinkImages($value) {
$value = '<body>' . $value . '</body>' ?: '';
$crawler = new Crawler($value, 'http://localhost');
// Remove all Icon_External_Link.png images.
$crawler->filter('img')->each(function (Crawler $crawler) {
foreach ($crawler as $node) {
$src = $node->getAttribute('src');
if (strstr($src, 'Icon_External_Link.png')) {
$node->parentNode->removeChild($node);
}
}
});
$value = $crawler->html();
$value = str_replace(['<body>', '</body>'], '', $value);
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment