<?php $html = '<img alt="äöü" class="foo" src="bar"><a href="target1">link text 1</a><a class="foo" src="target2">link text 2</a><div class="foo">xxx</div>'; $contentType = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; $doc = new DOMDocument('1.0'); $doc->loadHTML($contentType . $html, LIBXML_NOERROR); $xp = new DOMXPath($doc); foreach ($xp->query('//*[contains(@class, "foo") and (self::a or self::img)]') as $el) { echo $el->ownerDocument->saveHTML($el) . PHP_EOL; }
You have javascript disabled. You will not be able to edit any code.