Chciałbym zamienić wartość atrybutu "href" tagu "a" na wartość atrybutu "src" tagu "img". Przykład obecnego kodu:
Rezultat jaki chce osiągnąć to:
Proszę o pomoc, bo nie mam pojęcia jak to ugryźć.
Pozdrawiam.
$HTMLs = '<a class="image" href="http://example.com/index.php?title=File:example.jpg"> <img width="48" height="48" src="http://example.com/images/9/90/example.jpg" alt="Bombing.jpg"></img> </a>'; libxml_use_internal_errors(true); $html = new DOMDocument(); $html->validateOnParse = false; $html->loadHTML($HTMLs); foreach ( $html->getElementsByTagName('a') as $a) { }
function imageHref($content, $class) { libxml_use_internal_errors(true); $html = new DOMDocument(); $html->validateOnParse = false; $html->loadHTML($content); $finder = new DomXPath($html); $nodes = $finder->query('//a[@class="'.$class.'"]'); foreach ($nodes as $a) { foreach ($a->getElementsByTagName('img') as $a2) { $new = $a2->getAttribute('src'); $a->removeAttribute('href'); $a->setAttribute('href', $new); } } $content = $html->saveHTML(); return $content; }