Last active
April 24, 2024 17:33
-
-
Save m-manu/4152628 to your computer and use it in GitHub Desktop.
Useful CSV Parser for PHP 5.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Khuram007 I'm (pleasantly) surprised that people are still finding this useful after 11 years. 🙂