Menu
Hi,
I am using fgetcsv to read my csv files but i have a problem.
In the csv i have a description field, now that description field uses comma’s
The commas that need to be ignored have a infront of them and delimiter is a comma on its own.
Any idea how i can get it to ignore comma’s with a $ignoredelimiter variable in it
[code=php]
$ignoredelimiter = “”;
while (($line < $previewLimit) && ($data = fgetcsv($myFile, 1024, $delimiter)))
Many Thanks
Adam
<i>
</i>1. test
2. this is, a test
3. this is a "test", so to speak
<i>
</i>test,"this is, a test","this is a ""test"", so to speak"
the big problem is that unfortunatly i do not have control on the csv they are from a client? Stupid i know, but unfortunatly the way it is ?
Thanks for your help[/QUOTE]
[code=php]
<?php
# create test data:
$testFile = "test.csv";
$test =fopen($testFile, 'w') or die("unable to create test file");
for($i=1; $i<=3; $i++)
{
fputs($test, 'Line '.$i.',This is a "test".,It is, only, a test.'."n");
}
fclose($test);
# END create test data
### DEBUG
echo "<pre>TEST DATA:n";
readfile($testFile);
echo "</pre>";
### /DEBUG
# create temporary CSV file
$tmpCSV = "tmp".uniqid('X').".csv";
$tmp = fopen($tmpCSV, 'w') or die("unable to open temp file");
$csv = file($testFile) or die("Unable to read in CSV file");
foreach($csv as $key => $line)
{
$line = str_replace(',', '[comma]', trim($line));
$fields = explode(',', str_replace('"', '""', $line));
$out = '';
foreach($fields as $value)
{
$out .= '"'.str_replace('[comma]',',',$value).'",';
}
$out = rtrim($out, ',') . "n";
fputs($tmp, $out);
}
fclose($tmp);
# END create temporary CSV file
### DEBUG
echo "<pre>TMP FILE:n";
readfile($tmpCSV);
echo "</pre>";
### /DEBUG
# use the tmp file as our CSV file now:
$myFile = fopen($tmpCSV, 'r') or die("unable to open tmp csv file for reading");
# hard code vars for testing:
$line = 1;
$previewLimit = 2;
while (($line < $previewLimit) && $data = fgetcsv($myFile, 1024))
{
# do stuff with array $data here
# DEBUG
echo "<pre>";
print_r($data);
echo "</pre>n";
# /DEBUG
}
fclose($myFile);
# delete tmp file (and test data file):
unlink($tmpCSV);
unlink($testFile);
?>
[/code]
[code=php]
<?php
# create test data:
$testFile = "test.csv";
$test =fopen($testFile, 'w') or die("unable to create test file");
for($i=1; $i<=3; $i++)
{
fputs($test, 'Line '.$i.',This is a "test".,It is, only, a test.'."n");
}
fclose($test);
# END create test data
### DEBUG
echo "<pre>TEST DATA:n";
readfile($testFile);
echo "</pre>";
### /DEBUG
$csv = file($testFile) or die("Unable to read in CSV file");
foreach($csv as $line)
{
$fields = explode(',', str_replace(',', '[comma]', trim($line)));
foreach($fields as $key => $value)
{
$fields[$key] = str_replace('[comma]',',',$value);
}
$dataArray[] = $fields;
}
# END create temporary CSV file
### DEBUG
echo "<pre>$data array:n";
print_r($dataArray);
echo "</pre>";
### /DEBUG
# use the tmp file as our CSV file now:
$line = 1;
$previewLimit = 2;
while (($line < $previewLimit) && $data = array_shift($dataArray))
{
# do stuff with array $data here
# DEBUG
echo "<pre>";
print_r($data);
echo "</pre>n";
# /DEBUG
}
# delete test data file:
unlink($testFile);
?>
[/code]
[code=php]
function CreateTmpCSV($CSV) {
$tmpCSV = "tmp".uniqid('X').".csv";
$tmp = fopen($tmpCSV, 'w') or die("unable to open temp file");
$csv = file($CSV) or die("Unable to read in CSV file");
foreach($csv as $key => $line) {
$line = str_replace(',', '[comma]', trim($line));
$fields = explode(',', str_replace('"', '""', $line));
$out = '';
foreach($fields as $value){
$out .= '"'.str_replace('[comma]',',',$value).'",';
}
$out = rtrim($out, ',') . "n";
fputs($tmp, $out);
}
fclose($tmp);
return $tmpCSV;
}
[/code]
[code=php]
function CreateTmpCSV($CSV) {
$tmpCSV = "tmp".uniqid('X').".csv";
$tmp = fopen($tmpCSV, 'w') or die("unable to open temp file");
$csv = file($CSV) or die("Unable to read in CSV file");
foreach($csv as $key => $line) {
$line = str_replace(',', '[comma]', trim($line));
$fields = explode(',', $line);
$out = '';
foreach($fields as $value){
if ((substr($value, -1) == '"') && (substr($value, 0 ,1) == '"')) {
$out .= ''.str_replace('[comma]',',',$value).',';
} else {
$out .= '"'.str_replace('[comma]',',',$value).'",';
}
}
$out = rtrim($out, ',') . "n";
fputs($tmp, $out);
}
fclose($tmp);
return $tmpCSV;
}
[/code]
[code=php]
$string = str_replace("\n", "n", $string);
[/code]
[code=php]
$string = str_replace("\n", " ", $string);
[/code]
[code=php]
function CreateTmpCSV($CSV) {
// create tmp filename
$tmpCSV = "tmp".uniqid('X').".csv";
// open tmp CSV
$tmp = fopen($tmpCSV, 'w') or die("unable to open temp file");
// put the CSV contents into a variable
$csv = file($CSV) or die("Unable to read in CSV file");
// seperate every line of the CSV file
foreach($csv as $key => $line) {
// change the " to [speach] (mainly so we can run the query below)
$line = str_replace('"', '[speach]', trim($line));
// change the newlinecharactor to just a new line
$line = str_replace('\n', ' ', $line);
// variable to check if the csv file has the proper speach mark formation
$properCSV = (trim((substr($line, -8) == '[speach]')) && trim((substr($line, 0 ,8) == '[speach]')));
// if the csv file is with the proper speach marks
if ($properCSV) {
// If so then just explode where the , is surrounded by [speach] because otheriwse it will split if the field has a comma in the txt
$fields = explode('[speach],[speach]', $line);
} else { // as the document is inproper then : -
// replace the , for [comma], this is because the inproper csv tries to ignore , by puting a infront of it (we put it as [comma] so it dont get exploded)
$line = str_replace(',', '[comma]', trim($line));
// replace the [speach] for " so that we can locate and determine what ones to ignore (we can do this because the csv does not have the proper " formation)
$line = str_replace('[speach]', '"', trim($line));
$fields = explode(',', $line);
}
echo $line . '<br><br>';
$out = '';
// seperate every field of the CSV file
foreach($fields as $value) {
if ($properCSV) {
$out .= '"'.str_replace('[comma]',',',$value).'",';
// change the [speach] back to "
$out = str_replace('[speach]','"',$out);
// because it is the proper csv format, it already has " at the beginin and end of the $line and because we have added another one we have doubles at begining and end of $line.
$out = str_replace('""','"',$out);
} else {
$out .= '"'.str_replace('[comma]',',',$value).'",';
// change the [speach] back to "
$out = str_replace('[speach]','"',$out);
}
}
// remove the last comma (i think)??
$out = rtrim($out, ',') . "n";
// create the tmp CSV
fputs($tmp, $out);
// for test purposes only, remove once it is fully working
echo $out;
}
// close the link tmp CSV
fclose($tmp);
// return the filename
return $tmpCSV;
}
[/code]
0.1.9 — BETA 5.18