note 122696 modified in function.fgetcsv by cmb

From: Date: Wed, 07 Oct 2020 06:49:38 +0000
Subject: note 122696 modified in function.fgetcsv by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215537@lists.php.net to get a copy of this message
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; } ?> --was-- 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) !== $fp) { // 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; } ?> http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#215537) next »