If you load your HTML into a DOM document, you can use Xpath to query nodes from it.
All a elements inside the document:
That have an ancestor/a parent div element:
With the class attribute category-map second-links
//a[ancestor::div[@class = "category-map second-links"]]
Get the href attributes of the filtered a elements (Optionally)
//a[ancestor::div[@class = "category-map second-links"]]/@href
Full Example:
$html = <<<'HTML'
<div class="category-map second-links">
*****
<!--<div class="category-map second-links"> Comment hacks -->
<div class="category-map second-links">
<a href='xxx'>yyy</a>
<a href='zzz'>www</a>
...
</div>
<div class="category-map second-links">
*****
<!--<div class="category-map second-links"> Comment hacks -->
<div class="category-map second-links">
<a href='aaa'>bbb</a>
<a href='ccc'>ddd</a>
...
</div>
</div> <p class="sec">
HTML;
$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);
// fetch the href attributes
$hrefs = array();
foreach ($xpath->evaluate('//a[ancestor::div[@class = "category-map second-links"]]/@href') as $node) {
$hrefs[] = $node->value;
}
var_dump($hrefs);
// fetch the a elements an read some data from them
$linkData = array();
foreach ($xpath->evaluate('//a[ancestor::div[@class = "category-map second-links"]]') as $node) {
$linkData[] = array(
'href' => $node->getAttribute('@href'),
'text' => $node->nodeValue,
);
}
var_dump($linkData);
// fetch the a elements and store their html
$links = array();
foreach ($xpath->evaluate('//a[ancestor::div[@class = "category-map second-links"]]') as $node) {
$links[] = $dom->saveHtml($node);
}
var_dump($links);