Created
October 29, 2010 21:46
-
-
Save stevelacey/654499 to your computer and use it in GitHub Desktop.
PHP: Simple HTML Table Scrape
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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