PHP DOM Parser is very good at dealing with XML and HTML. It travels based on tree-structure and loads the data into DOM object. The first thing you need to do is to construct a dom document object and then load the html content in it.

// a new dom object
$dom = new domDocument;

// load the html into the object
$dom->loadHTML($html);

// discard white space
$dom->preserveWhiteSpace = false;

There are two important functions that can be used to extract contents from the html structure:

  1. getElementsByTagName
  2. getElementById

Get Elements by Tag Name

The function getElementsByTagName returns an array of objects.

$tables = $dom->getElementsByTagName('table');

foreach($tables as $table)
{
echo $dom->saveHTML($table);
}

The saveHTML function gets the exact html inside that particular node. To get the total number of elements, you the length attribute.

echo 'Found: ' . $tables->length . ' items';