title: PHP - Parse html to retrieve href from an "a" tag that is inside an other "a" tag
body: <p>I've been searching for hours (there shouldn't be any duplicate) and tried many different ways using both regex (regular expressions) and DOMdocument without success.</p>
  
  <p><strong>How the non-standard html code looks like:</strong></p>
  
  <pre><code>&lt;a class="SOMECLASS" href="javascript:__FUNCTION(SOME_HREF_INSIDE)" onclick="SOME_JS_FUNCTION();" id="SOME_ID" style="SOME_STYLE"&gt;
      &lt;a href="SOME_URL_3"&gt;SOME TEXT&lt;/a&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>Now the problem is I'm trying to get the url "SOME_URL_3" and both when parsing using regex or DOMdocument, the pasing stops as soon as it encounters the first href. Of course as the second "a" tag is part of the first one, the parser only see it as one. </p>
  
  <p><strong>I observed that browsers seems to automatically separate the tags when parsing as follow:</strong></p>
  
  <p><strong>Before:</strong></p>
  
  <pre><code>&lt;a href="SOME_URL"&gt;
      &lt;a href="SOME_URL_2"&gt;
      &lt;/a&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p><strong>After:</strong></p>
  
  <pre><code>&lt;a href="SOME_URL"&gt;
  &lt;/a&gt;
  &lt;a href="SOME_URL_2"&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>I've not been able to replicate this browsers behavior using php.</p>
  
  <p><strong>What I have tried that came closer to work:</strong></p>
  
  <pre><code>$dom = new DOMDocument();
  @$dom-&gt;loadHTML($result);
  
  foreach($dom-&gt;getElementsByTagName('a') as $link) { 
      $href_count = 0;
      $attrs = array();
  
      for ($i = 0; $i &lt; $link-&gt;attributes-&gt;length; ++$i) {
          $node = $link-&gt;attributes-&gt;item($i);
          if ($node-&gt;nodeName == "href") {
              $attrs[$node-&gt;nodeName][$href_count] = $node-&gt;nodeValue;
              $href_count++;
              if ($href_count &gt;= 2) {
                  echo "A second href has been found";
              }
          }
      }
  
      echo "&lt;pre&gt;";
      var_dump($attrs);
      echo "&lt;/pre&gt;";
  }
  </code></pre>
  
  <p>As you may expect it unfortunately doesn't work, in that case I wouldn't be here asking for help...</p>
  
  <p>Please don't hesitate to share your knowledge, any help or suggestion will be greatly appreciated!</p>
  
  <hr>
  
  <h1><strong>Update</strong></h1>
  
  <p>I had forgotten to specify in my initial question that the answer should still allow to capture standard href. My goal is to "extend" or "improve" my actual html parser to ensure I'm also retrieving the urls from any href. My initial code was only using RegEx and I wasn't able to capture second href from nested "a" tags. A perfect answer would allow to capture both nested and standard href. Brandon White's solution is perfect for nested href only but it would be resource consuming to use two different RegEx (nested/standard) to parse the entire html content twice. An ideal solution would be a RegEx allowing to capture both at the same time, if this is possible.</p>