Voting

: one plus one?
(Example: nine)

The Note You're Voting On

shaun at slickdesign dot com dot au
7 years ago
When a BOM character is suppled, `fgetscsv` may appear to wrap the first element in "double quotation marks". The simplest way to ignore it is to progress the file pointer to the 4th byte before using `fgetcsv`.

<?php
// BOM as a string for comparison.
$bom = "\xef\xbb\xbf";

// Read file from beginning.
$fp = fopen($path, 'r');

// Progress file pointer and get first 3 characters to compare to the BOM string.
if (fgets($fp, 4) !== $bom) {
// BOM not found - rewind pointer to start of file.
rewind($fp);
}

// Read CSV into an array.
$lines = array();
while(!
feof($fp) && ($line = fgetcsv($fp)) !== false) {
$lines[] = $line;
}
?>

<< Back to user notes page

To Top