note 19838 added to function.fgetcsv
| From: | I_love_brianda at my_sexy_wife dot heaven | Date: | Tue, 12 Mar 2002 21:27:45 +0000 |
| Subject: | note 19838 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-27683@lists.php.net to get a copy of this message | ||
<?php
/*
Example: CSV Element Conversion
- Read in a CSV File
- Init Caps certain columns
- Write out to a new CSV File
*/
$dir = "/websites/fm.lfcity.org/htdocs/csv/";
$file = "mine-MEX00-AC.csv";
$row = 1;
$fp = fopen ($file,"r");
while ($line = fgetcsv ($fp, 1000, ",")) {
$row++;
$num = count ($line);
for ($c=0; $c < $num; $c++) {
//print $data[$c] . "<br>";
$companies[$row][] = $line[$c];
}
}
fclose ($fp);
// Open Output CSV file
$new_file = $dir . "new_" . $file;
$fp = fopen($new_file, "w");
foreach($companies as $company){
// Do case conversion on specific columns
$company[1] = ucwords(strtolower($company[1]));
$company[2] = ucwords(strtolower($company[2]));
$company[4] = ucwords(strtolower($company[4]));
$company[9] = ucwords(strtolower($company[9]));
$company[10] = ucwords(strtolower($company[10]));
// Add quotes to lines containing commas
for($x=0; $x<=count($company); $x++){
if(strstr($company[$x],",")){
$company[$x] = '"'.$company[$x] .'"';
}
}
// Make new CSV line
$csv_line = implode(",", $company) . "\n";
// Write out new CSV Line entry to CSV file
fputs($fp, $csv_line);
}
fclose($fp);
?>
--
http://www.php.net/manual/en/function.fgetcsv.php
http://master.php.net/manage/user-notes.php?action=edit+19838
http://master.php.net/manage/user-notes.php?action=delete+19838
http://master.php.net/manage/user-notes.php?action=reject+19838