<?php
$html = <<< HTML
<html>
<base>
<base href="http://example.com"/>
</head>
<body>
<a href="//foo">foo</a>
<a href="bar">bar</a>
<a href="http://somewhere.else">somewhere else</a>
<a href="tel://911">halp!</a>
<a href="mailto:spam.pls@example.com">spam me</a>
<a href="javascript:alert('hi')">say hi</a>
</body>
</html>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$base = $xpath->evaluate('string(/html/head/base/@href)');
foreach ($xpath->evaluate('//a/@href') as $link) {
if (strpos($link->nodeValue, 'http') === 0) {
echo $link->nodeValue, PHP_EOL;
} elseif (strpos($link->nodeValue, '//') === 0) {
echo $base, substr($link->nodeValue, 1), PHP_EOL;
} elseif (strpos($link->nodeValue, ':') === FALSE) {
echo $base, '/', $link->nodeValue, PHP_EOL;
}
}
- Output for 5.6.0 - 5.6.27, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 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.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- http://example.com/foo
http://example.com/bar
http://somewhere.else
preferences:
214.79 ms | 404 KiB | 205 Q