MisterPi MisterPi - 18 days ago 8
PHP Question

How to get all HTML links from the DOMDocument?

I'm trying to fetch all HTML links from a document using the native DOM extension:

$items = $xpath->query('//div[@class="cards"]/div[@class="card"]/div/a[@class="card-click-target"]');


HTML code is:

<div class="cards">
<div class="card">
<div><a class="card-click-target" href="http://">Link</a></div>
</div>
</div>


But it gives me an empty object. How to do it correctly?

Answer

If you want to fetch the a nodes having href attribute, use //a[@href] XPath expression, e.g.:

$r = $xpath->evaluate('//a[@href]');
foreach ($r as $n) {
  printf("%s: %s\n", $n->textContent, $n->getAttribute('href'));
}

Sample Output

Link: http://domain.com/page

If, however, you want the href attribute values, use //a/@href selector:

$r = $xpath->evaluate('//a/@href');
foreach ($r as $n) {
  var_dump($n->value);
}

Example fetching all a tags with class attribute value equal to card-click-target:

$r = $xpath->evaluate('//a[@class = "card-click-target" and @href]');
foreach ($r as $n) {
  printf("%s: %s\n", $n->textContent, $n->getAttribute('href'));
};
Comments