- substr_count: documentation ( source)
- print_r: documentation ( source)
- preg_match_all: documentation ( source)
<?php
$result = <<<HTML
<a href="SOME_URL">
<a href="SOME_URL_2">
</a>
</a>
<a href="SOME_URL3">
<a href="SOME_URL_4">
</a>
</a>
<a href="SOME_URL_5">
</a>
<a href="SOME_URL_6">
</a>
HTML;
$dom = new DOMDocument();
@$dom->loadHTML($result);
foreach($dom->getElementsByTagName('a') as $link) {
$tag_html = $dom->saveHTML($link); //Get tag inner html
if (substr_count($tag_html, "href") > 1) { //If tag contains more than one href attribute
preg_match_all('/href="([^"]*)"/is', $tag_html, $link_output, PREG_SET_ORDER);
$output[] = $link_output[1][1]; //Output second href
} else { //Not nested tag
$output[] = $link->getAttribute('href'); //Output first href
}
}
echo "<pre>";
print_r($output);
echo "</pre>";