Page 1 of 1

fgetcsv vs fget??

Posted: Tue Jan 15, 2013 10:30 pm
by KerryH
Hi
I had a piece of code to put a csv file into a multidimensional array. Then I found out that the files I will process are txt file with records in the file separated by broken bars " | ". (e.g. 1000007414¦Abela¦John¦Andrew¦MR¦01-JUL-1983¦1¦1¦4¦2079¦2¦19¦¦1101¦42849231751¦ U)

Can someone show me how to 'fget' the txt files?

This is the code I had for csv files -

Code: Select all

if (($handle = fopen("file1.csv", "r")) !== FALSE) {
    # Set the parent multidimensional array key to 0.
    $nn1 = 0;
    while (($data = fgetcsv($handle, 0, "|")) !== FALSE) {
        # Count the total keys in the row.
        $c1 = count($data);
        # Populate the multidimensional array.
        for ($x=0;$x<$c1;$x++)
        {
            $csvarray1[$nn1][$x] = $data[$x];
			echo "$csvarray1($nn1)($x) = ". $csvarray1[$nn1][$x] . "<br />\n";
			echo "<br />\n";
        }
        $nn1++;
		echo "<br />\n";
    }
    # Close the File.
    fclose($handle);
}
Now the file is txt file, I should change "fgetcsv($handle,,)" to something else?

Code: Select all

if (($handle = fopen("personQh0001.txt", "r")) !== FALSE) {
    # Set the parent multidimensional array key to 0.
    $nn1 = 0;
    while (($data = fgetcsv($handle, 0)) !== FALSE) {
        # Count the total keys in the row.
        $c1 = count($data);
        # Populate the multidimensional array.
        for ($x=0;$x<$c1;$x++)
        {
            $csvarray1[$nn1][$x] = $data[$x];
			//echo "$csvarray1($nn1)($x) = ". $csvarray1[$nn1][$x] . "<br />\n";
			//echo "<br />\n";
        }
        $nn1++;
		//echo "<br />\n";
    }
    # Close the File.
    fclose($handle);
}

Re: fgetcsv vs fget??

Posted: Tue Jan 15, 2013 11:42 pm
by requinix
The name of the file doesn't matter: if it contains something-separated values, like comma-separated or pipe-separated, then fgetcsv() will work just fine.

Re: fgetcsv vs fget??

Posted: Wed Jan 16, 2013 11:43 am
by Christopher
requinix is correct. A .csv file is a text file that contain lines of delimited text. The .csv extension is to signify that it is a specific type of text file.

Re: fgetcsv vs fget??

Posted: Wed Jan 16, 2013 9:29 pm
by KerryH
requinix wrote:The name of the file doesn't matter: if it contains something-separated values, like comma-separated or pipe-separated, then fgetcsv() will work just fine.
Thanks. But I think fgetcsv doesn't seem to recognise ' | ' ? or did I do anything wrong with the syntax?

Code: Select all

while (($data = fgetcsv($handle, 0, "|")) !== FALSE) {


I got this -

Array(0)(0) = 1000003463¦Abraham¦Irene¦¦MRS¦21-MAR-1921¦2¦2¦4¦7010¦¦19¦¦1101¦40108887240¦ U

In previous file (csv file), I got this -

Array(0)(0) = 1000000001

Array(0)(1) = CRUISE

Array(0)(2) = TOM

Array(0)(3) = BOB

Array(0)(4) = MR

Array(0)(5) = 10-10-1999

Array(0)(6) = 2

Array(0)(7) = 2

Array(0)(8) = 4

Array(0)(9) = 8010

Array(0)(10) = 20

Array(0)(11) = 9225

Array(0)(12) = 41735383093112012

Array(0)(13) = QSM2992 GOLD

Array(0)(14) = 200917574KA

Re: fgetcsv vs fget??

Posted: Wed Jan 16, 2013 9:34 pm
by KerryH
Got it working now! Sorry. Thank you guys.

Re: fgetcsv vs fget??

Posted: Thu Jan 17, 2013 2:08 pm
by Weirdan
What was the problem?

Re: fgetcsv vs fget??

Posted: Thu Jan 17, 2013 2:45 pm
by requinix
It looks like the CSV is delimited with a U+00A6 BROKEN BAR, not the U+007C VERTICAL LINE that you can find on the keyboard. Which also explains the UTF-8 symptoms shown in

Code: Select all

Array(0)(0) = 1000003463¦Abraham¦Irene¦¦MRS¦21-MAR-1921¦2¦2¦4¦7010¦¦19¦¦1101¦40108887240¦ U
For the record, if altering the "C"SV to use a vertical line as the delimiter wasn't an option, I'd say that fgetcsv() is awesome and it'd be better to write a quick stream filter to utf8_decode() the text as it comes in (which is a bit less than easy but I won't write it out here since it's unnecessary).