<?php $html = <<<HTML <div> <img src="/relative/url/img.jpg" /> <form action="/"></form> <form action="/contact-us/"></form> <a href='/relative/url/'>Note the Single Quote</a> <img src="//example.com/protocol-relative-img.jpg" /> </div> HTML; $domain = '//example.com'; $targets = [ "//img[not(starts-with(@src, '//'))]", "//form[not(starts-with(@action, '//'))]", "//a[not(starts-with(@href, '//'))]" ]; $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); foreach ($xpath->query(implode('|', $targets)) as $node) { if ($src = $node->getAttribute('src')) { $node->setAttribute('src', $domain . $src); } elseif ($action = $node->getAttribute('action')) { $node->setAttribute('action', $domain . $action); } else { $node->setAttribute('href', $domain . $node->getAttribute('href')); } } echo $dom->saveHTML();
You have javascript disabled. You will not be able to edit any code.