/** * Extract table market from an HTML document starting from $target. * Search for $target and extract the table $numShifts after the text. * If $numShifts is negative, counts backwards. * * example: Extract table containing anchor: ($src,$target,-1) * example: Extract very next table after anchor: ($src,$target,0) * example: Skip next table, extract second: ($src,$target,1) * * @param string &$src Given HTML to search. * @param string $target What to look for in html (case insensitive) * @param int $numShifts Number of tables to count. * @return string HTML containing the requested table. */ function extractTableHTML(&$src, $target, $numShifts = 0) { $start = stripos($src, $target); if ($start === false) { return false; } if ($numShifts >= 0) { // Find tables after anchor do { $start = stripos($src, '<table', $start + 1); $numShifts -= 1; } while ($numShifts >= 0 && $start !== false); if ($start !== false) { $tmp = substr($src, $start); $tabs = HTMLTable::extractTablesHTML($tmp); return count($tabs) > 0 ? $tabs[0] : false; } } else { // find tables before anchor $stack = array(); $ix = -1; while ($ix !== false && $ix <= $start) { $ix = stripos($src, '<table', $ix + 1); $stack[] = $ix; } array_pop($stack); $top = false; while (!empty($stack) && $numShifts < 0) { $top = array_pop($stack); $numShifts += 1; } if ($top !== false && $numShifts >= 0) { $tmp = substr($src, $top); $tabs = HTMLTable::extractTablesHTML($tmp); return count($tabs) > 0 ? $tabs[0] : false; } } return false; }