note 113184 deleted from domdocument.getelementsbytagname by peehaa
| From: | peehaa@php.net | Date: | Wed, 11 Sep 2013 07:55:09 +0000 |
| Subject: | note 113184 deleted from domdocument.getelementsbytagname by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196221@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Here is a function to convert the populated tables in an HTML document into an array.
<?php
//Create an array containing all the populated tables in an HTML page
//Returned array: tables_to_array[table number][row number][column number]
function tables_to_array ($url) {
$htmlDocDom = new DOMDocument();
@$htmlDocDom->loadHTMLFile($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++;
}
$htmlDocRowCount++;
}
}
if ($htmlDocRowCount > 1) $tableCounter++;
}
return($htmlDocTableArray);
}
?>