Home » Php » php – Use fgetcsv for tab delimited file

php – Use fgetcsv for tab delimited file

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&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).

Answer:

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
}

Answer:

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

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