title: Getting element inside other element by class php DOMDocument
body: <p>Hi Guys i do have this Html Code :</p>
  
  <pre><code>&lt;div class="post-thumbnail2"&gt;
     &lt;a href="http://example.com" title="Title"&gt;
         &lt;img src="http://linkimgexample/image.png" alt="Title"/&gt;
     &lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I want to get the value of src image (<a href="http://linkimgexample/image.png" rel="nofollow">http://linkimgexample/image.png</a>) and the value of the href link (<a href="http://example.com" rel="nofollow">http://example.com</a>) using php DOMDocument</p>
  
  <p>what i did to get the link was something like that :</p>
  
  <pre><code>$divs = $dom-&gt;getElementsByTagName("div");
  
      foreach($divs as $div) { 
          $cl = $div-&gt;getAttribute("class");
  
          if ($cl == "post-thumbnail2") {
              $links = $div-&gt;getElementsByTagName("a");
              foreach ($links as $link)
                      echo $link-&gt;getAttribute("href")."&lt;br/&gt;";
          }
      }
  </code></pre>
  
  <p>i could do the same for src img</p>
  
  <pre><code>$imgs = $div-&gt;getElementsByTagName("img"); 
  foreach ($imgs as $img)
      echo $img-&gt;getAttribute("src")."&lt;br/&gt;";
  </code></pre>
  
  <p>but sometime in the website there is no image and the Html code is like that :</p>
  
  <pre><code> &lt;div class="post-thumbnail2"&gt;
     &lt;a href="http://example.com" title="Title"&gt;&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>so my questions is how could i get the 2 value at the same time it means when there is no image i show some message </p>
  
  <p>to be more clear this is an example :</p>
  
  <pre><code>&lt;div class="post-thumbnail2"&gt;
         &lt;a href="http://example1.com" title="Title"&gt;
             &lt;img src="http://linkimgexample/image1.png" alt="Title"/&gt;
         &lt;/a&gt;
      &lt;/div&gt;
  &lt;div class="post-thumbnail2"&gt;
         &lt;a href="http://example2.com" title="Title"&gt;&lt;/a&gt;
  &lt;/div&gt;
  &lt;div class="post-thumbnail2"&gt;
         &lt;a href="http://example3.com" title="Title"&gt;
             &lt;img src="http://linkimgexample/image2.png" alt="Title"/&gt;
         &lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>i want the result to be</p>
  
  <pre><code>http://example1.com - http://linkimgexample/image1.png
  http://example2.com - there is no image here !
  http://example3.com - http://linkimgexample/image2.pn
  </code></pre>