0

My html

<td class="end">
<span class="shopName">ダイワンテレコム</span><a href="http://kakaku.com/shop/3211/?used_pdid=K0000575280&amp;uctrl=85427"><img src="http://img1.kakaku.k-img.com/images/itemlist/itemv_btn_sinfo_l.gif" width="102" height="28" alt="詳細を見る"></a>
</td>

And I want to use DOMDocument to get http://kakaku.com/shop/3211/?used_pdid=K0000575280&amp;uctrl=85427

My php is

$atag = $td->getElementsByTagName("a");
$shop_url=$atag->getAttribute("href"); //PHP Fatal error:  Call to undefined method DOMNodeList::getAttribute() in C:\xampp\htdocs\wp-content\themes\theme-child\cellphone.php on line 172
John
  • 3,888
  • 11
  • 46
  • 84

2 Answers2

0

You have put foreach for getElementsByTagName its, a domlist object. Below code will be return all links.

<?php
$atag = $td->getElementsByTagName("a");

foreach( $atag  as $searchNode )
{
    echo $searchNode->getAttribute("href"); 
}
?>
mertizci
  • 538
  • 3
  • 12
0

Since getElementsByTagName return a Traversable object (DOMNodeList), you can access to the first element with

$atag[0]->getAttribute("href");

or loop them all with

foreach ($atag as $node) {
    //$node->getAttribute("href"); 
}
Federkun
  • 36,084
  • 8
  • 78
  • 90