title: php find image link with anchor inside a div with specific class name
body: <p>I need to find the image link and a <code>href</code> inside a specific <code>div</code>. The <code>div</code> has a class name. I have customized following code but cant get those images only inside a div with class name media. Here is an example of the HTML structure</p>
  <pre><code>&lt;div class=&quot;media&quot;&gt;
     &lt;a href=&quot;http://www.images.img/image.jpg&quot; title=&quot;&quot;&gt;
          &lt;img id=&quot;image&quot; src=&quot;http://www.images.img/zoomin/image.jpg.jpg&quot; alt=&quot;&quot; title=&quot;&quot; /&gt;    
  </code></pre>
  
  <p>Here is my code snippet:</p>
  <pre><code>foreach ($xpath-&gt;query('//div[class=&quot;media&quot;]//a[@href]//img') as $img) {
  
          for ($link = $img; $link-&gt;tagName !== 'a'; $link = $link-&gt;parentNode);
          
          $output[] = array(
              'href' =&gt; $link-&gt;getAttribute('href'),
              'src'  =&gt; $img-&gt;getAttribute('src'),
              'alt'  =&gt; $img-&gt;getAttribute('alt'),
          );
      }
  </code></pre>
  <p>But it finds all images with the anchor tag and any <code>div</code>.</p>
  <p>Can somebody provide some help?</p>