Table To Csv

public function tables_to_array($url)
{
$htmlDocDom = new DOMDocument();

@$htmlDocDom->loadHTML($url);
$htmlDocDom->preserveWhiteSpace = false;
$tableCounter = 0;
$htmlDocTableArray = array();
$htmlDocTables = $htmlDocDom->getElementsByTagName(‘table’);
foreach ($htmlDocTables as $htmlDocTable)
{
$htmlDocTableArray[$tableCounter] = array();
$htmlDocRows = $htmlDocTable->getElementsByTagName(‘tr’);
$htmlDocRowCount = 0;
$htmlDocTableArray[$tableCounter] = array();
foreach ($htmlDocRows as $htmlDocRow)
{
if (strlen($htmlDocRow->nodeValue) > 1)
{
$htmlDocColCount = 0;
$htmlDocTableArray[$tableCounter][$htmlDocRowCount] = array();
$htmlDocCols = $htmlDocRow->getElementsByTagName(‘td’);
foreach ($htmlDocCols as $htmlDocCol)
{
$htmlDocTableArray[$tableCounter][$htmlDocRowCount][] = $htmlDocCol->nodeValue;
$htmlDocColCount++;
}
$htmlDocCols = $htmlDocRow->getElementsByTagName(‘th’);
foreach ($htmlDocCols as $htmlDocCol)
{
$htmlDocTableArray[$tableCounter][$htmlDocRowCount][] = $htmlDocCol->nodeValue;
$htmlDocColCount++;
}

$htmlDocRowCount++;
}
}

$tableCounter++;
}
return ($htmlDocTableArray);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s