1

Could someone please help me out.

I'm trying to get multiple href's from a page for exmaple.

The page

<div class="link__ttl">
    <a href="/watch-link-53767-934537" target="_blank" rel="nofollow">Version 1</a>
</div>
<div class="link__ttl">
    <a href="/watch-link-53759-934537" target="_blank" rel="nofollow">Version 1</a>
</div>

PHP Dom

$data = array();

$data['links'] = array();

$page = $this->curl->get($page);
$dom = new DOMDocument();
@$dom->loadHTML($page);

$divs = $dom->getElementsByTagName('div'); 
for($i=0;$i<$divs->length;$i++){   
    if ($divs->item($i)->getAttribute("class") == "link__ttl") {
        foreach ($divs as $div) {
            $link = $div->getElementsByTagName('a');
            $data['links'][] = $link->getAttribute("href");
        }
    }
}

But this don't same to work and i get a error

Call to undefined method DOMNodeList::getAttribute()

Could someone help me out here please thanks

1
  • $div->getElementsByTagName('a') gives you a DOMNodeList; not a DOMNode. You'll need to access an specific item of the DOMNodeList or iterate though its items. Commented May 11, 2014 at 19:52

3 Answers 3

1

You're testing divs for having the link__tt class, but then just for each all the divs. Take only the anchors from the divs that have the class.
Then you're trying to call getAttribute from a DOMNodeList, you need to get the underlying domnode to get the attribute.

$divs = $dom->getElementsByTagName('div'); 
for($i=0;$i<$divs->length;$i++){  
    $div = $divs->item($i);
    if ($div->getAttribute("class") == "link__ttl") {
            $link = $div->getElementsByTagName('a');
            $data['links'][] = $link->item(0)->getAttribute("href");
    }
}

Another solution is to use xpath

$path = new DOMXPath($dom);
$as = $path->query('//div[@class="link__ttl"]/a'); 
for($i=0;$i<$as->length;$i++){  
    $data['links'][] = $as->item($i)->getAttribute("href");
}

http://codepad.org/pX5qA1BB

Sign up to request clarification or add additional context in comments.

Comments

1

$link = $div->getElementsByTagName('a'); retrieves a LIST of Items where you cant's get an attribute-value "href" of...

try use of $link[0] instead of $link

Comments

0

Any part of a DOM is an node. The attributes are nodes, too, not just the elements. Using Xpath you can directly fetch an list of href attribute nodes.

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);

$result = [];
foreach ($xpath->evaluate('//div[@class = "link__ttl"]/a/@href') as $href)  {
  $result[] = $href->value;
}

var_dump($result);

Output: https://eval.in/150202

array(2) {
  [0]=>
  string(24) "/watch-link-53767-934537"
  [1]=>
  string(24) "/watch-link-53759-934537"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.