You are here: I want to write a function that removes the second CSV column of its content « PHP Programming Language « IT news, forums, messages
I want to write a function that removes the second CSV column of its content

Posted by comp.lang.php on 12/17/39 11:56

I thought my function would have accomplished just that:

[PHP]
if (!function_exists('resetCSV')) {
function &resetCSV($fullFileName, $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_exists('check_csv_field_ref'))
@include_once(actual_path("$path/functions.inc.php"));
$contents = @file_get_contents(actual_path($fullFileName));
$fileID = @fopen(actual_path($fullFileName), 'r+');
// OPEN FOR READING AND WRITING
if (function_exists('check_csv_field_ref') &&
is_file(actual_path($fullFileName)) && strlen($contents) > 0) {
if ($willClearFile) {
// EMPTY THE FILE ENTIRELY
@fputs($fileID, '');
} else {
while (($data = @fgetcsv($fileID, 65536)) !== false)
@array_insert($data, $csvArray[]); // READ EACH ROW UP
TO 65K IN DATA
for ($i = 0; $i < @sizeof($csvArray); $i++) {
$csvArray[$i][1] = '';
// NULLIFY (DO NOT UNSET)
SECOND COLUMN
@array_walk($csvArray[$i], create_function('&$a',
'@check_csv_field_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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация