mkswanson Posted September 28, 2011 Share Posted September 28, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/247999-convert-html-table-to-comma-separated-values/ Share on other sites More sharing options...
oyster12 Posted September 28, 2011 Share Posted September 28, 2011 as i think, convert the file type into suitable type is the best way to do it. else, you can get the HTML content by a little php code, and then insert to the database Quote Link to comment https://forums.phpfreaks.com/topic/247999-convert-html-table-to-comma-separated-values/#findComment-1273410 Share on other sites More sharing options...
cunoodle2 Posted September 28, 2011 Share Posted September 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247999-convert-html-table-to-comma-separated-values/#findComment-1273415 Share on other sites More sharing options...
mkswanson Posted September 28, 2011 Author Share Posted September 28, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/247999-convert-html-table-to-comma-separated-values/#findComment-1273549 Share on other sites More sharing options...
cunoodle2 Posted September 28, 2011 Share Posted September 28, 2011 Post the code you have so far and I'll take a look Quote Link to comment https://forums.phpfreaks.com/topic/247999-convert-html-table-to-comma-separated-values/#findComment-1273559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.