title: How can I extract all img tag within an anchor tag?
body: <p>I would like to extract all img tags that are within an anchor tag using the PHP DOM object.</p>
  
  <p>I am trying it with the code below but its getting all anchor tag and making it's text empty due the inside of an img tag.</p>
  
  <pre><code>function get_links($url) {
  
      // Create a new DOM Document to hold our webpage structure
      $xml = new DOMDocument();
  
      // Load the url's contents into the DOM
      @$xml-&gt;loadHTMLFile($url);
  
      // Empty array to hold all links to return
      $links = array();
  
      //Loop through each &lt;a&gt; tag in the dom and add it to the link array
      foreach($xml-&gt;getElementsByTagName('a') as $link) 
      {
         $hrefval = '';
         if(strpos($link-&gt;getAttribute('href'),'www') &gt; 0)
         {
             //$links[] = array('url' =&gt; $link-&gt;getAttribute('href'), 'text' =&gt; $link-&gt;nodeValue);
             $hrefval = '#URL#'.$link-&gt;getAttribute('href').'#TEXT#'.$link-&gt;nodeValue;
             $links[$hrefval] = $hrefval;
         }
         else
         {
             //$links[] = array('url' =&gt; GetMainBaseFromURL($url).$link-&gt;getAttribute('href'), 'text' =&gt; $link-&gt;nodeValue);
             $hrefval = '#URL#'.GetMainBaseFromURL($url).$link-&gt;getAttribute('href').'#TEXT#'.$link-&gt;nodeValue;
             $links[$hrefval] = $hrefval;
         }
      }
  
      foreach($xml-&gt;getElementsByTagName('img') as $link) 
      {
          $srcval = '';
         if(strpos($link-&gt;getAttribute('src'),'www') &gt; 0)
         {
             //$links[] = array('src' =&gt; $link-&gt;getAttribute('src'), 'nodval' =&gt; $link-&gt;nodeValue);
             $srcval = '#SRC#'.$link-&gt;getAttribute('src').'#NODEVAL#'.$link-&gt;nodeValue;
             $links[$srcval] = $srcval;
         }
         else
         {
             //$links[] = array('src' =&gt; GetMainBaseFromURL($url).$link-&gt;getAttribute('src'), 'nodval' =&gt; $link-&gt;nodeValue);    
             $srcval = '#SRC#'.GetMainBaseFromURL($url).$link-&gt;getAttribute('src').'#NODEVAL#'.$link-&gt;nodeValue;
             $links[$srcval] = $srcval;
  
         }
      }
  
      //Return the links
      //$links = unsetblankvalue($links);
      return $links;
  } 
  </code></pre>
  
  <p>This returns all anchor tag and all img tag separately.</p>