I am trying to display all the links that are located in <div class="post">. There are many <div class="post"> inside my page (it is a blog). However there are many links in every <div class="post"> but I want to display the first of every div.
My question is how can I limit my code to show only the first one and continue to the next div?
Below is my code that gets and displays all the links. Gracias!
<?php
$dom = new DOMDocument;
libxml_use_internal_errors(TRUE);
$dom->loadHTMLFile('http://www.mydomain.com/');
libxml_clear_errors();
$xPath = new DOMXPath($dom);
$links = $xPath->query('//div[@class="post"]/a/@href');
foreach($links as $link) {
printf("%s \n", $link->nodeValue);
}
?>
🙂