Skip to content
Tags

Programatically edit a CSV file

by moflint on October 24th, 2011

Here’s an example of adding a leading zero to columns 2 and 3 of the CSV file:

$input = fopen('numbers.csv','r');
$output = fopen('numbers_touched.csv','w');
while($csv_line = fgetcsv($input,1024)) {
	$line = array();
	foreach ($csv_line as $k => $field){
		if ($k == 2 || $k == 3) {
			$field = '0' . $field;
		}
		$line[] = $field;
	}
	fputcsv($output,$line);
}
fclose($input);
fclose($output);

From → PHP

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS