- error_reporting: documentation ( source)
<?php
error_reporting(E_ALL);
$htmlString = <<<_HTML
<div>
<a href="A"><img src="X"></a>
<a href="C"><img src="X"></a>
<a href="D"><img src="X"></a>
<a href="E"><img src="X"></a>
<a href="F"><img src="X"></a>
<p>
<a href="G"><img src="X"></a>
</p>
<a href="H">No image in this one</a>
</div>
_HTML;
$doc = new DOMDocument();
$doc->loadHTML($htmlString, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($doc);
$links = $xpath->query('//a[@href and img]');
foreach ($links as $link) {
$link->setAttribute('href', 'B');
}
$newHtmlString = $doc->saveHTML();
echo $newHtmlString;