Skip to content

Instantly share code, notes, and snippets.

@stevelacey
Created October 29, 2010 21:46
Show Gist options
  • Save stevelacey/654499 to your computer and use it in GitHub Desktop.
Save stevelacey/654499 to your computer and use it in GitHub Desktop.
PHP: Simple HTML Table Scrape
<?php
$doc = new DOMDocument();
// It's rare you'll have valid XHTML, suppress any errors- it'll do its best.
@$doc->loadhtml($string);
$xpath = new DOMXPath($doc);
// Modify the XPath query to match the content
foreach($xpath->query('//table')->item(0)->getElementsByTagName('tr') as $rows) {
$cells = $rows->getElementsByTagName('td');
// Do stuff with the data
echo $cells->item(0)->textContent;
echo $cells->item(1)->textContent;
echo $cells->item(2)->textContent;
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment