3v4l.org

run code in 300+ PHP versions simultaneously
<?php $homepage = "https://example.com/"; $html = '<img class="drt" src="100.png"><img src="../101.png"><img src="/102.png"><img src="103.png">'; $dom = new DOMDocument(); $dom->loadHTML($html); foreach ($dom->getElementsByTagName('img') as $img) { $src = $img->getAttribute('src'); $src = $homepage.basename($src); $img->setAttribute('src', $src); } // hacky way! remove unwanted doctype ect $ret = preg_replace('~<(?:!DOCTYPE|/?(?:html|body|head))[^>]*>\s*~i', '', $dom->saveHTML()); echo trim(str_replace('<meta http-equiv="Content-Type" content="text/html;charset=utf-8">', '', $ret)); // proper way! but you dont have correct DOM, no <body> // remove <!DOCTYPE //$dom->removeChild($dom->doctype); // remove <html><body></body></html> //$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); // //echo $dom->saveHTML();
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
<img class="drt" src="https://example.com/100.png"><img src="https://example.com/101.png"><img src="https://example.com/102.png"><img src="https://example.com/103.png">

preferences:
62.81 ms | 1534 KiB | 4 Q