3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML <body> <img src="http://example/img/product/name/thumbs/100/img.jpg" alt="lol" width="100" height="100" caption="false" class="popup-img left" /> </body> HTML; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); $nodes = $xpath->query('//img[contains(@class, "popup-img")]'); foreach($nodes as $node) { $a = $dom->createElement('a'); $a->setAttribute('class', $node->getAttribute("class")); $a->setAttribute('title', $node->getAttribute("alt")); $href = parse_url($node->getAttribute("src"), PHP_URL_PATH); $a->setAttribute('href', '..' . str_replace('/thumbs/100/', '/', $href)); $a->setAttribute('style', "background: url('..$href'); -webkit-background-size:cover; background-size:cover;"); $node->parentNode->insertBefore($a, $node); $node->parentNode->removeChild($node); } echo $dom->saveHTML();

preferences:
49.68 ms | 402 KiB | 5 Q