Skip to content

Instantly share code, notes, and snippets.

@pbsladek
Created June 14, 2014 19:18
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 pbsladek/ca98fdde7d1da4533088 to your computer and use it in GitHub Desktop.
Save pbsladek/ca98fdde7d1da4533088 to your computer and use it in GitHub Desktop.
Special character issues reading csv. ASCII with UTF-8 fgetcsv not working.
$csvheader = NULL;
$data = array();
if (($handle = fopen($argv[1], "r")) !== FALSE) {
while (($row = fgets($handle, 4096)) !== FALSE) {
if (!$csvHeader) {
//format csv header row into array of strings
$row = str_replace('"', '', $row);
$row = str_replace("\r\n", '', $row);
$num = strlen($row) - strlen(str_replace(",", "", $row));
$csvHeader = array();
$csvHeader = explode( ",", $row , ($num+1) );
} else {
//line that fixed my issue
$row = utf8_encode($row);
$Data = str_getcsv($row, "\n");
foreach($Data as &$row){
$row = str_getcsv($row, ",");
$data[] = array_combine($csvHeader, $row);
}
}
}
fclose($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment