I'm outputting all the links on a page, and I want to make them hyperlinks.
The problem is I can't control the source document's URLs. Sometimes they're relative, sometimes they're absolute, and sometimes they're mixed.
Is there a simple way to make all relative links absolute, while still retaining links that were absolute in the first place?
I've looked at other questions and they seem to be focused on using regex or something similar to add the site domain to the beginning of the URL, which would work fine if all the URLs were relative.
If it matters, here's the code I'm using:
$html = file_get_contents(-[my url]-);
$doc = new DOMDocument();
libxml_use_internal_errors(true); //to hide error messages
$doc->loadHTML($html);
libxml_clear_errors();
$links = $doc->getElementsByTagName('a');
foreach($links as $link)
{
echo $link->getAttribute('href') . '<br />';
}