I want to write a function that removes the second CSV column of its content

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    I want to write a function that removes the second CSV column of its content

    I thought my function would have accomplished just that:

    [PHP]
    if (!function_exis ts('resetCSV')) {
    function &resetCSV($full FileName, $path, $willClearFile = false) {


    /*------------------------------------------------------------------------------------------------------------------------------------------
    New 8/23/2006: This function will either reset the second column
    of a CSV file (clearing the column of values) or clear file if
    $willClearFile is true to prevent user data from potentially
    going to another client

    -------------------------------------------------------------------------------------------------------------------------------------------*/
    if (!function_exis ts('check_csv_f ield_ref'))
    @include_once(a ctual_path("$pa th/functions.inc.p hp"));
    $contents = @file_get_conte nts(actual_path ($fullFileName) );
    $fileID = @fopen(actual_p ath($fullFileNa me), 'r+');
    // OPEN FOR READING AND WRITING
    if (function_exist s('check_csv_fi eld_ref') &&
    is_file(actual_ path($fullFileN ame)) && strlen($content s) 0) {
    if ($willClearFile ) {
    // EMPTY THE FILE ENTIRELY
    @fputs($fileID, '');
    } else {
    while (($data = @fgetcsv($fileI D, 65536)) !== false)
    @array_insert($ data, $csvArray[]); // READ EACH ROW UP
    TO 65K IN DATA
    for ($i = 0; $i < @sizeof($csvArr ay); $i++) {
    $csvArray[$i][1] = '';
    // NULLIFY (DO NOT UNSET)
    SECOND COLUMN
    @array_walk($cs vArray[$i], create_function ('&$a',
    '@check_csv_fie ld_ref(trim($a) );'));
    fputs($fileID, trim(join(',', $csvArray[$i])));
    if ($_ENV['windir'] || $_SERVER['windir']) fputs($fileID,
    "\r\n"); else fputs($fileID, "\n");
    }
    }
    }
    @fclose($fileID );
    }
    }

    [/PHP]

    However, as it does remove the second column of the CSV file, it fails
    to overwrite the existing content of the CSV file with the
    newly-reformed content.

    Which is the best approach to accomplish rewriting CSV content this
    way?

    Thanx
    Phil

  • NC

    #2
    Re: I want to write a function that removes the second CSV column of its content

    comp.lang.php wrote:
    >
    Which is the best approach to accomplish rewriting CSV
    content this way?
    You need to change your approach. Write reformatted lines into a new
    file; when finished, delete the old file and rename the new one, so it
    has the same name the old one did.

    Cheers,
    NC

    Comment

    Working...