src/CsvLib.php
/**
     * Remove columns from a CSV file.
     * @param string $filepath - the path of the CSV file we are modifying.
     * @param array $columnIndexes - array of integers specifying the column indexes to remove, starting at 1
     * @param string $delimiter - optionally specify the delimiter if it isn't a comma
     * @param string $enclosure - optionally specify the enclosure if it isn't double quote
     */
Remove columns from a CSV file.
@param string $filepath - the path of the CSV file we are modifying.
@param array $columnIndexes - array of integers specifying the column indexes to remove, starting at 1
@param string $delimiter - optionally specify the delimiter if it isn't a comma
@param string $enclosure - optionally specify the enclosure if it isn't double quote
Remove columns from a CSV file . @param string $filepath - the path of the CSV file we are modifying . @param array $columnIndexes - array of integers specifying the column indexes to remove starting at 1 @param string $delimiter - optionally specify the delimiter if it isn ' t a comma @param string $enclosure - optionally specify the enclosure if it isn ' t double quote
public static function removeColumns($filepath, array $columnIndexes, $delimiter=",", $enclosure='"')
    {
        $tmpFile = tmpfile();
        $fileHandle = fopen($filepath, "r");
        $lineNumber = 1;
        if ($fileHandle)
        {
            while (!feof($fileHandle))
            {
                $lineArray = fgetcsv($fileHandle, 0, $delimiter, $enclosure);
                if (!empty($lineArray))
                {
                    foreach ($columnIndexes as $columnHumanIndex)
                    {
                        $columnIndex = $columnHumanIndex - 1;
                        if (!isset($lineArray[$columnIndex]))
                        {
                            $msg = "removeColumns: source CSV file does not have " . 
                                   "column: " . $columnIndex . " on line: " . $lineNumber;
                            throw new \Exception($msg);
                        }
                        unset($lineArray[$columnIndex]);
                    }
                    fputcsv($tmpFile, $lineArray, $delimiter, $enclosure);
                }
                $lineNumber++;
            }
            fclose($fileHandle);
            $meta_data = stream_get_meta_data($tmpFile);
            $tmpFileName = $meta_data["uri"];
            rename($tmpFileName, $filepath); 
        }
        else
        {
            throw new \Exception("removeColumns: failed to open CSV file for trimming.");
        }
    }
public static function removeColumns ( $ filepath , array $ columnIndexes , $ delimiter = " , " , $ enclosure = '"' ) { $ tmpFile = tmpfile ( ) ; $ fileHandle = fopen ( $ filepath , " r " ) ; $ lineNumber = 1 ; if ( $ fileHandle ) { while ( ! feof ( $ fileHandle ) ) { $ lineArray = fgetcsv ( $ fileHandle , 0 , $ delimiter , $ enclosure ) ; if ( ! empty ( $ lineArray ) ) { foreach ( $ columnIndexes as $ columnHumanIndex ) { $ columnIndex = $ columnHumanIndex - 1 ; if ( ! isset ( $ lineArray [ $ columnIndex ] ) ) { $ msg = " removeColumns: source CSV file does not have  " . " column:  " . $ columnIndex . "  on line:  " . $ lineNumber ; throw new \ Exception ( $ msg ) ; } unset ( $ lineArray [ $ columnIndex ] ) ; } fputcsv ( $ tmpFile , $ lineArray , $ delimiter , $ enclosure ) ; } $ lineNumber ++ ; } fclose ( $ fileHandle ) ; $ meta_data = stream_get_meta_data ( $ tmpFile ) ; $ tmpFileName = $ meta_data [ " uri " ] ; rename ( $ tmpFileName , $ filepath ) ; } else { throw new \ Exception ( " removeColumns: failed to open CSV file for trimming. " ) ; } }
Remove columns from a CSV file.
Remove columns from a CSV file .
# replace the old upload file with new.