Skip to content

Instantly share code, notes, and snippets.

@lancewillett
Last active July 28, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancewillett/0abd78f0425eec5d4ff4b29cc6de8c58 to your computer and use it in GitHub Desktop.
Save lancewillett/0abd78f0425eec5d4ff4b29cc6de8c58 to your computer and use it in GitHub Desktop.
PHP: parse a CSV formatted file (comma-delimited)
<?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