Skip to content

Instantly share code, notes, and snippets.

@m-manu
Last active April 24, 2024 17:33
Show Gist options
  • Save m-manu/4152628 to your computer and use it in GitHub Desktop.
Save m-manu/4152628 to your computer and use it in GitHub Desktop.
Useful CSV Parser for PHP 5.2
<?php
/*
Copyright (c) 2013, Manu Manjunath
All rights reserved.
Redistribution and use of this program in source/binary forms, with or without modification are permitted.
Link to this gist is preferred, but not a condition for redistribution/use.
*/
function parse_csv_file($csvfile) {
$csv = Array();
$rowcount = 0;
if (($handle = fopen($csvfile, "r")) !== FALSE) {
$max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000;
$header = fgetcsv($handle, $max_line_length);
$header_colcount = count($header);
while (($row = fgetcsv($handle, $max_line_length)) !== FALSE) {
$row_colcount = count($row);
if ($row_colcount == $header_colcount) {
$entry = array_combine($header, $row);
$csv[] = $entry;
}
else {
error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount");
return null;
}
$rowcount++;
}
//echo "Totally $rowcount rows found\n";
fclose($handle);
}
else {
error_log("csvreader: Could not read CSV \"$csvfile\"");
return null;
}
return $csv;
}
@m-manu
Copy link
Author

m-manu commented Apr 24, 2024

@Khuram007 I'm (pleasantly) surprised that people are still finding this useful after 11 years. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment