Jump to content

Parsing a TSV Tab Seperated File - Weird Ouput.


CoDeRs

Recommended Posts

Hey Guys,

 

I am at work and i have to create a script to parse data from a TSV file into a database. The TSV file has to be sifted though before it is updated as they only want specific values in the database. Connecting to the DB and Using SQL is no problem for me. However for some reason my script is not outputting the data the way i want. Right now i am using fgets and explode to put the data into a 2D array, i have also used fgetcsv and i get the same results. Please see comments on where my errors are.

 

Script demo output is http://www.codernetworks.com/Magellon/upload.php

 

I feel that the code snipit below is very simple.

 

<?php
setlocale(LC_ALL, 'en_US.UTF-8'); //Tryed this as i was reading online. 

$nFileName = "ParentFolder/UnitFolder/TestFile.tsv"; //Hidden due to security reasons.
$nRow = 1;

$nFile = fopen($nFileName, "r");

if ($nFile !== FALSE) {
    while (!feof($nFile)) {

$nLineData = fgets($nFile);

//echo "$nLineData<br>"; //Debug, Works Fine. 

$nParsed = explode("\t", $nLineData, -1);

echo "Parsed Line - " & $nParsed[0] & "<br>"; //Debug, Outputs Junk (eg Line 4 = @P)
echo "<br> Parsed Line - $nParsed[0] <br>"; //Debug, Outputs Proper (eg Line 4 = #START)
       
$nCols = count($nParsed); 
        $nRow++;

        for ($i=0; $i < $nCols; $i++) {
	//Splitting Data into a 2D Array
        	$nData[$nRow][$i] = $nParsed[$i];

        }
    }
echo "Processing Data<br>";
echo "-----------------------------------------------------<br>";

// Outputing Data from the 2D Array - Only receiving what i call junk.
echo "Test Time Stamp -  " & $nData[1][1] & "<br>";
echo "Test Time Stamp -  " & $nData[2][1] & "<br>";
echo "Test Time Stamp -  " & $nData[3][1] & "<br>";
echo "Test Time Stamp -  " & $nData[4][1] & "<br>";
echo "Test Time Stamp -  " & $nData[5][1] & "<br>";
echo "Test Time Stamp -  " & $nData[6][1] & "<br>";
echo "Test Time Stamp -  " & $nData[7][1] & "<br>";
    fclose($nFile);
}
?>

The ampersand does not concatenate, the dot does.

echo "Parsed Line - " . $nParsed[0] . "<br>";

echo "Test Time Stamp -  " . $nData[1][1] . "<br>";

As your own example output shows your second example line works just fine. Your example proves what works yet you want to follow a different path???

 

 

HTH

Teamatomic

So very sorry, too many languages running around with the hamster. They get all jumbled.

 

No the reason is because putting a 2D array within quotes was not working. So i had to separate the strings. And using the wrong joining symbol caused all the errors. Something so simple and so obvious... yet it eluded me all day.

 

I feel soo embarrassed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.