note 113184 added to domdocument.getelementsbytagname
| From: | Anonymous | Date: | Tue, 10 Sep 2013 07:53:13 +0000 |
| Subject: | note 113184 added to domdocument.getelementsbytagname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196210@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 217.37.99.129
----
Manual Page -- http://php.net/manual/en/domdocument.getelementsbytagname.php
Edit -- https://master.php.net/note/edit/113184
Del: integrated -- https://master.php.net/note/delete/113184/integrated
Del: useless -- https://master.php.net/note/delete/113184/useless
Del: bad code -- https://master.php.net/note/delete/113184/bad+code
Del: spam -- https://master.php.net/note/delete/113184/spam
Del: non-english -- https://master.php.net/note/delete/113184/non-english
Del: in docs -- https://master.php.net/note/delete/113184/in+docs
Del: other reasons-- https://master.php.net/note/delete/113184
Reject -- https://master.php.net/note/reject/113184
Search -- https://master.php.net/manage/user-notes.php