Last active
August 22, 2016 07:43
-
-
Save bmunslow/000a0689bb3d583f20d58dfe32c30f97 to your computer and use it in GitHub Desktop.
Drupal: Programmatically read and process CSV file using Feeds CSV parser
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 | |
/** | |
* Quick CSV parsing in Drupal 7, using CSV parser library in the Feeds module. | |
* Default encoding expected to be UTF8. | |
* | |
*/ | |
// Custom variables, adjust to your needs | |
$file_name = 'public://custom_path_to_my_file.csv' | |
$delimiter = ';'; | |
$limit = 0; | |
$start = 0; | |
// Load and configure parser. | |
feeds_include_library('ParserCSV.inc', 'ParserCSV'); | |
$parser = new ParserCSV(); | |
$parser->setDelimiter($delimiter); | |
$iterator = new ParserCSVIterator($file_name); | |
$parser->setLineLimit($limit); | |
$parser->setStartByte($start); | |
$rows = $parser->parse($iterator); | |
/** | |
* At this point, $rows is an associative array with the following structure, where each value represents a column in the CSV file | |
* [0]=> string | |
* [1]=> string | |
* ... | |
* [n]=> string | |
*/ | |
foreach ($rows as $row) { | |
// Custom processing | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment