HTMLページからすべてのURLを次のような配列に選択したい:
This is a webpage <a href="http://somesite.com/link1.php">with</a>
different kinds of <a href="http://somesite.com/link1.php"><img src="someimg.png"></a>
私が望む出力は次のとおりです。
with => http://somesite.se/link1.php
今私は得る:
<img src="someimg.png"> => http://somesite.com/link1.php
with => http://somesite.com/link1.php
start と end の間に画像を含む URL/リンクは必要ありません。文字のあるもののみ。
私の現在のコードは次のとおりです。
<?php
function innerHTML($node) {
$ret = '';
foreach ($node->childNodes as $node) {
$ret .= $node->ownerDocument->saveHTML($node);
}
return $ret;
}
$html = file_get_contents('http://somesite.com/'.$_GET['apt']);
$dom = new DOMDocument;
@$dom->loadHTML($html); // @ = Removes errors from the HTML...
$links = $dom->getElementsByTagName('a');
$result = array();
foreach ($links as $link) {
//$node = $link->nodeValue;
$node = innerHTML($link);
$href = $link->getAttribute('href');
if (preg_match('/\.pdf$/i', $href))
$result[$node] = $href;
}
print_r($result);
?>