fgetcsv vs fget??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KerryH
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2013 10:14 pm

fgetcsv vs fget??

Post 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);
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fgetcsv vs fget??

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: fgetcsv vs fget??

Post 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.
(#10850)
KerryH
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2013 10:14 pm

Re: fgetcsv vs fget??

Post 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
KerryH
Forum Newbie
Posts: 5
Joined: Wed Jan 09, 2013 10:14 pm

Re: fgetcsv vs fget??

Post by KerryH »

Got it working now! Sorry. Thank you guys.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: fgetcsv vs fget??

Post by Weirdan »

What was the problem?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fgetcsv vs fget??

Post 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).
Post Reply