Last active
July 28, 2017 14:37
-
-
Save lancewillett/0abd78f0425eec5d4ff4b29cc6de8c58 to your computer and use it in GitHub Desktop.
PHP: parse a CSV formatted file (comma-delimited)
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 | |
/** | |
* This file is use to parse a CSV formatted file (comma-delimited). | |
*/ | |
// File should be first and only argument. | |
if ( empty( $argv[1] ) ) { | |
die( 'Please supply at least one filename.' ); | |
} | |
parse_csv_file( $argv[1] ); | |
function parse_csv_file( $file ) { | |
$results = array(); | |
$f = fopen( $file, "r" ); | |
while ( $line = fgets( $f ) ) { | |
$fields = explode( ',', rtrim( $line ) ); | |
} | |
// Simple print array | |
echo "<pre>"; | |
print_r( $fields ); | |
echo "</pre><br>"; | |
fclose( $f ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment