title: scrape href attribute value from inside spans with ID
body: <p>OK folks the page Im trying to scrape has the following structure</p>
  
  <pre><code>&lt;span id="1"&gt;
      &lt;a href="https://example.com"&gt;+&lt;/a&gt;
      &lt;span title="1"&gt;DATA HERE&lt;/span&gt;
      &lt;a href="https://example.com"&gt;DATA HERE&lt;/a&gt; 
      &lt;a href="https://example.com"&gt;DATA HERE&lt;/a&gt;
  &lt;/span&gt;
  &lt;span id="2"&gt;
      &lt;a href="https://example.com"&gt;+&lt;/a&gt;
      &lt;span title="1"&gt;DATA HERE&lt;/span&gt;
      &lt;a href="https://example.com"&gt;DATA HERE&lt;/a&gt; 
      &lt;a href="https://example.com"&gt;DATA HERE&lt;/a&gt;
  &lt;/span&gt;
  </code></pre>
  
  <p>There are 128 records on the page ( spans with ID values )</p>
  
  <p>I am using the following code but its saving the data which is great but i need each a href attribute value seperated by a , until it gets to the last one inside the span with the ID and then I need to to have PHP_EOL to move to a new line</p>
  
  <p>Please help I'm pulling hair out</p>
  
  <p>Code :</p>
  
  <pre><code>do {
      foreach($doc-&gt;getElementsByTagName('span') as $element ) { 
  
          if (!empty($element-&gt;getAttribute('id'))){
  
              foreach($doc-&gt;getElementsByTagName('a') as $ahref ) {
  
                  if ($ahref-&gt;hasAttribute('href')) { 
                      $filename = 'test2/'.$f.'.txt';
                      $file = fopen($filename,"a");
  
                      $data = $ahref-&gt;getAttribute('href').',';
                      fwrite($file,$data);
                      fclose($file);
                  }
              }
          }
      }
  }
  </code></pre>