TFD3 Posted June 6, 2008 Share Posted June 6, 2008 I have a CSV file: http://www.spc.noaa.gov/climo/reports/today_torn.csv That i need to be converted to a .txt file using PHP. How would I go about doing this? Thanks, Kenny Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/ Share on other sites More sharing options...
Buddski Posted June 6, 2008 Share Posted June 6, 2008 It all depends on how you want the txt file to be layed out. Give us an example of what the text output will look like.. Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-558902 Share on other sites More sharing options...
TFD3 Posted June 6, 2008 Author Share Posted June 6, 2008 It all depends on how you want the txt file to be layed out. Give us an example of what the text output will look like.. Each line in the CSV file looks like this: Time,F-Scale,Location,County,State,Lat,Lon,Comments 1800,UNK,14 M SW Rush,...ect... It does not have to be a text file. This output of this php file would do just fine. I would like the order to be changed to the following with the added text: Icon: $Lon,$Lat,0,1,1,$Time,$Location,$County,$State,$comments So really all i need is the rows and columns to have separate "tags" as in $Lot $Lon ext.. Im working on this my self trying to get it to work but i have never used a CSV file using PHP before. http://www.alabamaweather.org/scripts/lsr/lsr.php Possible? Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-558913 Share on other sites More sharing options...
Buddski Posted June 6, 2008 Share Posted June 6, 2008 I had a little play around and managed to come up with this little function.. If you have any questions about it just ask..It might not be what your looking for but anyway.. <?php function convert_csv($csv) { if (file_exists($csv)) { $file = file($csv); $tags = array(0 => 'Time','F-Scale','Location','County','State','Lat','Lon','Comments'); $data = array(); $count = 0; foreach ($file as $key=>$value) { $row = explode(',',$value); foreach ($row as $k=>$v) { $data[$count][$tags[$k]] = $v; } $count++; } return $data; } else { return 'File not Found.'; } } $file = 'today_torn.csv'; $array = convert_csv($file); echo "<pre>"; print_r($array); echo "</pre>"; ?> It will ouput something simular to this.. Array ( [0] => Array ( [Time] => Time [F-Scale] => F-Scale [Location] => Location [County] => County [state] => State [Lat] => Lat [Lon] => Lon [Comments] => Comments ) [1] => Array ( [Time] => 1820 [F-Scale] => UNK [Location] => 11 S GRANADA [County] => PROWERS [state] => CO [Lat] => 37.9 [Lon] => -102.3 [Comments] => TORNADO OCCURRED FROM 1220 PM UNTIL AROUND 1223 PM. NO DAMAGE REPORTED WITH THE TORNADO WHICH PASSED ABOUT 300 FEET NORTHWEST OF A RANCH HOUSE IN OPEN COUNTRY. (PUB) ) [2] => Array ( [Time] => 1930 [F-Scale] => UNK [Location] => 14 SW RUSSELL SPRINGS [County] => LOGAN [state] => KS [Lat] => 38.77 [Lon] => -101.36 [Comments] => BRIEF TORNADO (GLD) ) There are a few things you could tweak in it like ignoring the first line of headings but you get the general idea... Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-558971 Share on other sites More sharing options...
Barand Posted June 6, 2008 Share Posted June 6, 2008 if you just want to process each line at a time, putting the elements into variables <?php $fh = fopen('my.csv', 'r'); while (list($Time,$fscale,$Location,$County,$State,$Lat,$Lon,$comments) = fgetcsv($fh, 1024)) { echo "$Lon, $Lat, 0, 1, 1, $Time, $Location, $County, $State, $comments <br/>"; } fclose($fh); ?> Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559020 Share on other sites More sharing options...
TFD3 Posted June 6, 2008 Author Share Posted June 6, 2008 if you just want to process each line at a time, putting the elements into variables <?php $fh = fopen('my.csv', 'r'); while (list($Time,$fscale,$Location,$County,$State,$Lat,$Lon,$comments) = fgetcsv($fh, 1024)) { echo "$Lon, $Lat, 0, 1, 1, $Time, $Location, $County, $State, $comments <br/>"; } fclose($fh); ?> That worked great but needs one minor adjustment. Here is what I get from the script above: Icon: Lat, Lon, 0, 1, 1, Time, Location, County, State, Comments Icon: 37.9, -102.3, 0, 1, 1, 1820, 11 S GRANADA, PROWERS, CO, TORNADO OCCURRED FROM 1220 PM UNTIL AROUND 1223 PM. NO DAMAGE REPORTED WITH THE TORNADO WHICH PASSED ABOUT 300 FEET NORTHWEST OF A RANCH HOUSE IN OPEN COUNTRY. (PUB) Ect... The first line displays (Icon: Lat, Lon, 0, 1, 1, Time, Location, County, State, Comments ) I need that to somehow disappear. Here is the output from the script above using CSV File http://www.spc.noaa.gov/climo/reports/yesterday_torn.csv :: http://www.alabamaweather.org/scripts/lsr/lsr.php <?php header("Content-type: text/plain"); $fh = fopen('http://www.spc.noaa.gov/climo/reports/yesterday_torn.csv', 'r'); while (list($Time,$fscale,$Location,$County,$State,$Lat,$Lon,$comments) = fgetcsv($fh, 1024, ",")) { echo "Icon: $Lat,$Lon,0,1,1,TIME: $Time\\n LOCATION: $Location\\n COUNTY: $County\\n STATE: $State\\n EF-Scale: $fscale\\n COMMENTS: $comments \n"; } fclose($fh); ?> Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559148 Share on other sites More sharing options...
Buddski Posted June 6, 2008 Share Posted June 6, 2008 This may not be the best method but im a little sleep deprived at present.. <?php $fh = fopen('my.csv', 'r'); $counter = 0; while (list($Time,$fscale,$Location,$County,$State,$Lat,$Lon,$comments) = fgetcsv($fh, 1024)) { if ($counter == 0) { continue; } echo "$Lon, $Lat, 0, 1, 1, $Time, $Location, $County, $State, $comments <br/>"; $counter++; } fclose($fh); ?> That should skip the first row. Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559190 Share on other sites More sharing options...
TFD3 Posted June 6, 2008 Author Share Posted June 6, 2008 This may not be the best method but im a little sleep deprived at present.. <?php $fh = fopen('my.csv', 'r'); $counter = 0; while (list($Time,$fscale,$Location,$County,$State,$Lat,$Lon,$comments) = fgetcsv($fh, 1024)) { if ($counter == 0) { continue; } echo "$Lon, $Lat, 0, 1, 1, $Time, $Location, $County, $State, $comments <br/>"; $counter++; } fclose($fh); ?> That should skip the first row. Thanks but that does not output anything for some reason http://www.alabamaweather.org/scripts/lsr/lsr.php Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559194 Share on other sites More sharing options...
TFD3 Posted June 6, 2008 Author Share Posted June 6, 2008 Im playing with changing numbers to see if that helps at all but when I change 0 to 1 and so on it shows all rows instead of all but the first one. <?php header("Content-type: text/plain"); echo "Title: Local Storm Reports Refresh: 15 Color: 255 255 255\n"; echo "IconFile: 1, 128, 128, 64, 64, http://www.grlevelxstuff.com/plugins/p13_download_manager/images/tb_571.png\n\n"; $fh = fopen('http://www.spc.noaa.gov/climo/reports/yesterday_torn.csv', 'r'); $counter = 0; while (list($Time,$fscale,$Location,$County,$State,$Lat,$Lon,$comments) = fgetcsv($fh, 1024, ",")) { if ($counter == 0) { continue; } echo "Icon: $Lat,$Lon,0,1,1,TIME: $Time\\n LOCATION: $Location\\n COUNTY: $County\\n STATE: $State\\n EF-Scale: $fscale\\n COMMENTS: $comments \n"; $counter++; } fclose($fh); ?> Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559287 Share on other sites More sharing options...
Barand Posted June 6, 2008 Share Posted June 6, 2008 try <?php $fh = fopen('http://www.spc.noaa.gov/climo/reports/yesterday_torn.csv', 'r'); $counter = 0; while (list($Time,$fscale,$Location,$County,$State,$Lat,$Lon,$comments) = fgetcsv($fh, 1024, ",")) { if ($counter++ > 0) echo "Icon: $Lat,$Lon,0,1,1,TIME: $Time\\n LOCATION: $Location\\n COUNTY: $County\\n STATE: $State\\n EF-Scale: $fscale\\n COMMENTS: $comments \n"; } fclose($fh); ?> $counter never got past zero in previous version Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559305 Share on other sites More sharing options...
ingeva Posted June 6, 2008 Share Posted June 6, 2008 I have a CSV file: A CSV file is a text file, and you can read it with any editor. To make a text editor the default handler for the file, just rename it and give it a .txt extension. If you want to keep the original, copy the file, giving the new file at .txt extension. Example: $file = file_get_contents ("csv-filename.csv"); file_put_contents ("txt-filename.txt",$file); It shouldn't be much more complicated than that. Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559318 Share on other sites More sharing options...
TFD3 Posted June 6, 2008 Author Share Posted June 6, 2008 Thanks guys! Link to comment https://forums.phpfreaks.com/topic/108937-solved-csv-to-txt-conversion-using-php/#findComment-559392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.