这将附加/test_image.jpg
到src
每个图像元素的pyro-image
.
演示
$html = '<p><img alt="twerk team" class="pyro-image" src="http://localhost/test/files/thumb/8/595" style="float: none;" /></p>';
$d = new DOMDocument();
$d->loadHTML($html);
$xp = new DomXPath($d);
$class = 'pyro-image';
$nodes = $xp->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]");
foreach($nodes as $n)
{
$n->setAttribute('src', (string)$n->getAttribute('src') . '/test_image.jpg');
echo $d->saveXML($n->parentNode);
// note - can use saveHTML() instead but only from PHP 5.3.6
}
输出
<p><img alt="twerk team" class="pyro-image" src="http://localhost/test/files/thumb/8/595/test_image.jpg" style="float: none;"/></p>