Last active
October 7, 2018 21:23
-
-
Save mmohiudd/5777780 to your computer and use it in GitHub Desktop.
Basic CSV parse function
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
function getCSVData($file){ | |
$info = pathinfo($file); | |
$fp = @fopen($file, "r"); | |
$data = array(); | |
$line = fgets($fp, 4096); // read the first line, headers | |
// use /[ "\']+/ to remove white spaces as well | |
$fields = preg_replace('/[ "\']+/', '', explode(",", trim($line))); | |
while (!feof($fp)) { // loop till end of file. | |
$line = fgets($fp, 4096); // read a line. | |
if (empty($line)) continue; | |
$field_values = array(); // will save all field values | |
$values = explode(",", trim($line)); | |
$entry = array(); | |
// only to sanitize the values | |
foreach($values as $i=>$value){ // fetch all value | |
$entry[$fields[$i]] = trim(str_replace('"', '', $value)); | |
} | |
$data[] = $entry; | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment