Jump to content

Convert HTML Table to Comma Separated Values


mkswanson

Recommended Posts

I have a file that is uploaded by the user that has a series of rows of data in an html table.

 

The columns are always a constant, but the number of rows in the file can change.  These rows of the html table are the only information in the file when I am processing it.

 

I need to either convert the file to a CSV file or write the values to a CSV list so that I can then insert them into a database.

 

Any suggestions would be wonderful...I've spent the last three hours spinning my wheels.

 

Here is an example of one row of data from the file:

 

<tr><td bgcolor=#dddddd class=dataFT >5 (Excellent)</td><td bgcolor=#dddddd class=dataFT >5 (Excellent)</td><td bgcolor=#dddddd class=dataFT >5 (Excellent)</td><td bgcolor=#dddddd class=dataFT > </td><td bgcolor=#dddddd class=dataFT > </td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >1015020</td><td bgcolor=#dddddd class=dataFT >155498322</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >4731751</td><td bgcolor=#dddddd class=dataFT >2011-09-25 11:18:33  (-5:00)</td><td bgcolor=#dddddd class=dataFT >2011-09-25 12:16:50  (-5:00)</td><td bgcolor=#dddddd class=dataFT >0:58:17</td><td bgcolor=#dddddd class=dataFT >0:58:16</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >DATA</td><td bgcolor=#dddddd class=dataFT >Resolved</td></tr>

Link to comment
Share on other sites

I know that there is a much more efficient way to do this but this is the way that I know best..

 

You will need to start off reading in the file.  I will assume that you know that and that you will store said items in the variable $contents.  You will need to modify the loop I use too.  This is very very rough code.

 

<?php

//start the CSV for your file
$csv_string = "";

//create a position counter for how far you are in the file
$position = 1;

//create a temp value to store temp strings in
$temp_string = "";

while (//you will need something here to determine the end but again that will depend on the method you are reading with)
{
//determine the starting location of the variable you are trying to strip
$string_begin = strpos($contents,"dataFT", $position);
$string_begin = strpos($contents,">", $string_begin) + 1;

//determine the ending location of the variable you are trying to strip
$string_end = strpos($contents,"<", $string_begin);

//now you have the positions so you can strip out the variables
$temp_string = substr($contents,$string_begin,$string_end-$string_begin);

//now clean up any white space on the variable and then add it on to the end of the csv variable.
$temp_string = trim($temp_string);
$csv_string .= $temp_string.", ";

//reset the position variable so you advance in the loop
$position = $string_end;

} //end of the loop

//echo out your item here..
echo $csv_string
?>

 

Again, I know that this is not the most efficient way to do it but it does get you the results you need.

Link to comment
Share on other sites

Thanks for the start here.  I've been trying to use get_file_contents to read in the file, but it seems to be dropping any of the characters I'm trying to use to parse this.  Is this because I should be using a different command?

 

Sorry if this is a dumb question - I don't have a lot of experience working with files since most of my work like this has been directly with a database and not an intermediate flat file.

 

Thanks for the assistance! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.