 <p>Turn the HTML into a <a href="http://php.net/manual/en/class.domdocument.php" rel="nofollow">DOMDocument</a>, then fetch the requested values using <a href="http://php.net/manual/en/class.domxpath.php" rel="nofollow">XPath</a> <a href="http://www.w3.org/TR/xpath/" rel="nofollow">queries</a>:</p> <pre><code>$html = &lt;&lt;&lt;EOS &lt;table id='table1'&gt; &lt;tr&gt; &lt;td&gt; &lt;div class='us'&gt; &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;div&gt; &lt;span&gt;text1&lt;/span&gt; &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;span&gt;text2&lt;/span&gt; &lt;/td&gt; &lt;td&gt;text3&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;div class='jo'&gt;&lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;div&gt; &lt;span&gt;text4&lt;/span&gt; &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;span&gt;text5&lt;/span&gt; &lt;/td&gt; &lt;td&gt;text6&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; EOS; // Load HTML into a DOMDocument $dom = new DOMDocument(); $dom-&gt;loadHTML($html); // Find div's with class attribute $xPath = new DOMXPath($dom); $divsWithClassAttribute = $xPath-&gt;query('//div[@class]'); // Loop through divs foreach ($divsWithClassAttribute as $div) { // Create row array starting with class name $row = array($div-&gt;getAttribute('class')); // Get siblings of the td parent of each div $siblings = $xPath-&gt;query('parent::td/following-sibling::td', $div); // Loop through (td) siblings and extract text content foreach ($siblings as $sibling) { $row[] = trim($sibling-&gt;textContent); // Add text value } // Output row echo implode(' - ', $row), PHP_EOL; } </code></pre> <p>Output:</p> <pre><code>us - text1 - text2 - text3 jo - text4 - text5 - text6 </code></pre>