Jump to content

Creating a CSV/Excel file from HTML page


IanArcher

Recommended Posts

I have extracted records from a database and stored them on an HTML page with only text. Each record is stored in a <p> paragraph field and separated by a line break <br /> and a line <hr> The page. For example: Company Name555-555-555Address Line 1Address Line 2Website: www.example.com I just need to place these records into a CSV file. I used fputcsv in combination with array() and file_get_contents() but it read my the entire source code of the webpage into a .csv file and alot of data was missing as well. These are multiple records stored in the same format. So after an entire record block as seen above, it is separate by an <hr> line tag. I want to read the company name into the Name column, the Phone number into the Phone column, the addresses into the Address column and the Website into the Website column as shown below.00Gxw.png I also have a copy of PHPExcel where i can use the following code to add data to cells. Even if i can get the data in .xls/xlsx format i can always save it into a .csv file format afterwards.

// Add some data$objPHPExcel->setActiveSheetIndex(0)			->setCellValue('A1', 'Hello')			->setCellValue('B2', 'world!')			->setCellValue('C1', 'Hello')			->setCellValue('D2', 'world!');

Though i see a possible means of entry if i can get the 'Hello world' words to become variables that define each line, but to be added to the Cells sequentially so data doesn't get written into the same cell.How can i do this?

Link to comment
Share on other sites

If the data is in a database then get the data from that instead of using file_get_contents to read a file. Get each row from the database, put the values into an array in the order you want them in the columns, and then use fputcsv.

Link to comment
Share on other sites

If the data is in a database then get the data from that instead of using file_get_contents to read a file. Get each row from the database, put the values into an array in the order you want them in the columns, and then use fputcsv.
I would've done just that, but the login credentials to access that database is lost, so that is why i used this method to extract the data.
Link to comment
Share on other sites

So instead of getting access to the database, you want to parse an HTML file to extract the data? Well, then your options are to either use regular expressions, or a combination of various string processing functions. Here's your regular expression function: http://www.php.net/m...g-match-all.php And more information: http://www.php.net/m...cre.pattern.php And here's the list of string functions: http://www.php.net/m...ook.strings.php A possible solution with those might include things like explode, strpos, substr, etc. It seems like a waste of time to help you write that code when that's not the solution to the actual problem (the problem is that you can't access the database), but if you want to figure out how to access the database then I'll help you with that. Also, they misspelled "Ahwatukee" in that directory name.

Link to comment
Share on other sites

So instead of getting access to the database, you want to parse an HTML file to extract the data? Well, then your options are to either use regular expressions, or a combination of various string processing functions. Here's your regular expression function: http://www.php.net/m...g-match-all.php And more information: http://www.php.net/m...cre.pattern.php And here's the list of string functions: http://www.php.net/m...ook.strings.php A possible solution with those might include things like explode, strpos, substr, etc. It seems like a waste of time to help you write that code when that's not the solution to the actual problem (the problem is that you can't access the database), but if you want to figure out how to access the database then I'll help you with that. Also, they misspelled "Ahwatukee" in that directory name.
Yeah i'm aware, i named it like that purposely.
Link to comment
Share on other sites

I have this code i am using right here

<?phpini_set('max_execution_time', 1000);  $str = file_get_contents('clients.html');// open a file pointer to csv$fp = fopen('records2.csv', 'w'); // first, split each record into a separate array element$records = explode('<hr>', $str); // then iterate over this arrayforeach ($records as $record) { 	// strip tags and trim enclosing whitespace	$stripped = trim(strip_tags($record)); 	// explode by end-of-line	$fields = explode(PHP_EOL, $stripped); 	// array walk over each field and trim whitespace	array_walk($fields, function(&$field) {		$field = trim($field);	}); 	// create row	$row = array(		$fields[0], // name		$fields[1], // phone		sprintf('%s, %s', $fields[2], $fields[3]), // address		$fields[6], // web	); 	// write cleaned array of fields to csv	fputcsv($fp, $row); } // donefclose($fp);  ?>

But it only puts the Name of the business in the first column, not every line in it's corresponding columns as they should. As opposed to

$list = array("Peter,Griffin,Oslo,Norway","Glenn,Quagmire,Oslo,Norway",);$file = fopen("contacts.csv","w");foreach ($list as $line)  {  fputcsv($file,split(',',$line));  }fclose($file);?>

Which does it the right way.Could someone show me the right way?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...