title: PHP DOM / XPath
body: <p>Hopefully should be a simple question for someone that has done it before!</p>
  
  <p>I have a list of old web documents in table format with lots of contact details in it. What I have managed so far is to create a PHP script that parses the XHTML doc and pull out old client contact details.</p>
  
  <p>An example of the document format:</p>
  
  <pre><code>&lt;tr&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;a href="#" class="details"&gt;Indigo Blue 123&lt;/a&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" align="top"&gt;&lt;font class="details"&gt;123 Blue House&lt;/font&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" align="top"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;font class="details"&gt;Hanley&lt;/font&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;font class="details"&gt;ST13 4SN&lt;/font&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;font class="details"&gt;Stoke on Trent&lt;/font&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;font class="details"&gt;01875 322511&lt;/font&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC" valign="top"&gt;&lt;a href="http://www.indigoblue123.org.uk" target="_blank" class="details"&gt;www.indigoblue123.org.uk&lt;/a&gt;&lt;/td&gt;
    &lt;td bgcolor="#CCCCCC"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>What I need to do is parse all of these contact details into an array. The few things that I'm not sure on how to complete is grabbing the empty blocks to be empty array entries (i.e. Address 2 and Address 3 will be blank but I need to know this) as well as grabbing the web address from the <code>&lt;a&gt;..&lt;/a&gt;</code> block.</p>
  
  <p>So far I have figured all populated data has <code>class=details</code> in some form. However, as I mentioned before I'm not sure what the best way to accomplish the overall result is. There around 20-40 entries in the different files I have.</p>
  
  <p>I have managed the basics with this so far:</p>
  
  <pre><code>&lt;?php
    print '&lt;pre&gt;';
    $html = file_get_contents('old-contacts.xhtml');
  
    // Create new DOM object:
    $dom = new DomDocument();
  
    // Load HTML code:
    $dom-&gt;loadHTML($html);
  
    $xpath = new DOMXPath($dom);
    $details = $xpath-&gt;query("//table/tbody/tr[td/font/@class = 'details']");
  
    for ($i = 0; $i &lt; $details-&gt;length; $i++) {
      $data[$i]['data'] = $details-&gt;item($i)-&gt;nodeValue;
      echo $data[$i]['data'];
    }
    print '&lt;/pre&gt;';
  ?&gt;
  </code></pre>
  
  <p>Any help would be great!</p>
  
  <p>Thanks</p>