MisterPi MisterPi - 4 months ago 27
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>

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


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) {

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'));