0

I'm trying to use Zend_Dom_Query to get some specific content from a webpage. I got a query working to get the content from one dom-element. Now i want to select a second dom-element to get this content also.

This is the html:

<div class="blocks">
  <div class="w2">
    <h2>Some title</h2>
    <p>some text</p>
    <p>more text</p>
    <p class="more-info"><a href="#">link</a></p>
  </div>
  <div class="w2">
    <h2>Some title</h2>
    <p>some text</p>
    <p>more text</p>
    <p class="more-info"><a href="#">link</a></p>
  </div>
</div>

My code so far:

$client = new Zend_Http_Client();
$client->setUri('http://awsomewebsite');
$result = $client->request('GET');
$response = $result->getBody();
$dom = new Zend_Dom_Query($response);
foreach ($dom->query('div.w2') as $content) {
  echo $content->getElementsByTagName('h2')->item(0)->nodeValue; // this gives me the h2 value
  echo $content->getElementsByTagName('a')->item(0)->getAttribute('href');
}

Now the problem is when there are more anchor links this solution isn't working. My question is: What is the correct way to select multiple elements? Or can i use a new query inside this foreach to select the right element?

1 Answers1

0
$i=0;
foreach ($dom->query('div.w2') as $content) {
  echo $content->getElementsByTagName('h2')->item($i)->nodeValue; // this gives me the h2 value
  echo $content->getElementsByTagName('a')->item($i)->getAttribute('href');
  $i++;
}
coolguy
  • 7,866
  • 9
  • 45
  • 71
  • This way i get all anchor links in div.w2, but i actually want only the anchor link in the p.more-info and only the first h2. – user2018548 Jan 29 '13 at 07:58