Home » Php » Use fgetcsv for tab delimited file

Use fgetcsv for tab delimited file

Posted by: admin November 29, 2017 Leave a comment

Questions:

Is it possible to use fgetcsv in PHP to open a tab-delimited file?

Answers:
$csvData = fgetcsv($fileHandle, 0, "\t");

Where $fileHandle is a valid file handle. The 0 is just to tell the function not to limit seeking through lines (however you can change this to suit, the docs do say not imposing a limit decreases performance).

Questions:
Answers:

Make sure to use double quotes around the “\t”, single quotes will not work.

$fh = fopen($file, 'r');
while ($line = fgetcsv($fh, 0, "\t") !== false) {
    // do stuff
}

Questions:
Answers:

yes, you can specify tab “\t” in its parameters. see the doc.

while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE)