Jump to content

[SOLVED] CSV to TXT conversion using PHP?


TFD3

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.