<?php
$myurl = $_POST[url];
error_reporting(E_ALL);
//
// BIG FILE, we need some extra time to load it!
//
ini_set("max_execution_time", 120);
function getTable($url) {
//
// 1. Fetch content of page
//
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
//
// 2. Process content of page
//
// Remove everything before the table
$temp = preg_split("/section.34.*?<table[^>]*>/si", $content);
$content = $temp[1];
// Remove everything after table
$content = preg_replace("/<\/table>.*$/si", "", $content);
// Spit rows
$rows = preg_split("/<\s*tr[^>]*>/i", $content);
// Remove the first element, which doesn't contain a row
// and the second element which contains the header
array_shift($rows);
array_shift($rows);
//
// Process each row
//
$data = array();
$rowcount = 0;
foreach ($rows as $row) {
$data[$rowcount] = array();
// Remove HTML comments
$row = preg_replace("/<!--.*?-->/s", "", $row);
// Convert to spaces
$row = preg_replace("/ /i", " ", $row);
// Split row to cells
$cells = preg_split("/<\s*td[^>]*>/i", $row);
// Remove first element, which doesn't contain a cell
array_shift($cells);
// Process each cell
$cellcount = 0;
$cell_value = "";
$field_count = "";
foreach ($cells as $cell) {
// Convert <BR>'s to spaces
$cell = preg_replace("/<br>/i", " ", $cell);
// Remove HTML-tags
$cell = preg_replace("/<.*?>/s", "", $cell);
// Remove starting and trailing whitespace
$cell = ltrim(rtrim($cell));
// Remove multiple spaces
$cell = preg_replace('/\s\s+/', ' ', $cell);
// Store cell contents in $data[row][column]
$cell = preg_replace('/\'/', ' ', $cell);
$data[$rowcount][$cellcount] = $cell;
$cell_value .= !empty($cell) ? "'".$cell."'~" : "NULL~";//get the cell value
if($cellcount == 0){$field_name = "FILE_NUMBER_$cellcount";}//and so on 11 times
if($cellcount == 1){$field_name = "APPLICANT_NAME_$cellcount";}
if($cellcount == 2){$field_name = "APPLICATION_TYPE_$cellcount";}
if($cellcount == 3){$field_name = "APPLICATION_RECEIVED_$cellcount";}
if($cellcount == 4){$field_name = "DEVELOPMENT_DESCRIPTION_$cellcount";}
if($cellcount == 5){$field_name = "DEVELOPMENT_LOCATION_$cellcount";}
if($cellcount == 6){$field_name = "DECISION_DATE_$cellcount";}
if($cellcount == 7){$field_name = "DEVELOPMENT_TYPE_$cellcount";}
if($cellcount == 8){$field_name = "PROTECTED_STRUCTURE_$cellcount";}
if($cellcount == 9){$field_name = "WASTE_LICENCE_$cellcount";}
if($cellcount == 10){$field_name = "RECEIVE_MARKETING_MATERIAL_$cellcount";}
$field_count .= $field_name."~";//and so on 11 times
$field_name = ""; //reset value
$cellcount++;
}
$field_count = rtrim($field_count,"~");//trim last ~
$cell_value = rtrim($cell_value,"~");//trim last ~
//here prepare the fields for insert and run mysql_query
echo "insert into(".str_replace("~",",",$field_count).") values(".str_replace("~",",",$cell_value).")replace<br>";
$cell_value = "";//clear values after insert
$field_count = "";//clear values after insert
$rowcount++;
}
return $data;
}
// The URL of the page which contains the table to get the data from
$url = "$myurl";
// getTable($url) returns an array containing all data from the table:
// $data[row][column] (both start at zero)
//
// for example:
// $data[0][0] the data from the first cell from the first row
// $data[2][4] the fifth cell from the third row
//
// to get the last cell from the last row:
// $rows = count($data);
// $cols = count($data[0]);
// $lastcell = $data[$rows-1][$cols-1];
//
$data = getTable($url);
// Lets dump all the contents of $data to see what's in it
// Look at the source of the page!!!
?>