Jump to content

fgetcsv returning odd results


griffen

Recommended Posts

I'm trying to read a CSV file. I've got an example line below of the file. What I'm finding is that it treats "tal. ,2 days,751" as a separate line. No idea why.

 

38936644,LG U8380  on Off-Peak 1000 (18 Mths),LG,"Monthly Charge: 20, Inclusive: 1000 cross-network Off-peak minutes, Phone Features: Downloadable Games etc, Bluetooth, Built in Camera, Polyphonic Ringtones, Picture Messaging, Triband","Immerse yourself in sound with the U8380's powerful stereo speakers. The U8380 enhances the whole 3G experience with stunning sound making video clips, music and ringtones better than ever before! Also boasting Bluetooth, megapixel camera technology and more makes this a great multimedia 3G handset.  Key Features  Dimensions: 95.7 x 49.5 x 23.7 mm Ringtones: Polyphonic (72 channels) Memory: 23 MB shared memory, Card Slot: microSD (TransFlash) up to 256MB Camera: 1.3 Mega Pixel, 1280 x 1024 pixels with video and flash MP3/AAC/MPEG4 player Bluetooth 3G Calendar Tri-Band GSM 900/GSM 1800/GSM 1900",http://www.test.com/store/fulldetails-r346212930-19984.ihtml,http://www.test.com/store/prodimg/8380large.jpg,,0,9 Months 1/2 Price Line Rental. ,2 days,751

 

I'm reading the file in through:

 

$fp = fopen($_FILES['fileupload']['tmp_name'], "r");

 

Then reading in each line:

 

while (!feof($fp)) {
        $fields = fgetcsv($fp, 1000, ",");

        for($i =0; $i < count($fields); $i++) {
                if($fields[$i] == NULL)
                        $fields[$i] = "blank";
                echo "Field $i == $fields[$i] <br />";
        }
        echo "================================<br />";
}

 

The output is looking like:

 

Field 0 == 38936644

Field 1 == LG U8380 on Off-Peak 1000 (18 Mths)

Field 2 == LG

Field 3 == Monthly Charge: 20, Inclusive: 1000 cross-network Off-peak minutes, Phone Features: Downloadable Games etc, Bluetooth, Built in Camera, Polyphonic Ringtones, Picture Messaging, Triband

Field 4 == Immerse yourself in sound with the U8380's powerful stereo speakers. The U8380 enhances the whole 3G experience with stunning sound making video clips, music and ringtones better than ever before! Also boasting Bluetooth, megapixel camera technology and more makes this a great multimedia 3G handset. Key Features Dimensions: 95.7 x 49.5 x 23.7 mm Ringtones: Polyphonic (72 channels) Memory: 23 MB shared memory, Card Slot: microSD (TransFlash) up to 256MB Camera: 1.3 Mega Pixel, 1280 x 1024 pixels with video and flash MP3/AAC/MPEG4 player Bluetooth 3G Calendar Tri-Band GSM 900/GSM 1800/GSM 1900

Field 5 == http://www.test.com/store/fulldetails-r346212930-19984.ihtml

Field 6 == http://www.test.com/store/prodimg/8380large.jpg

Field 7 == blank

Field 8 == 0

Field 9 == 9 Months 1/2 Price Line Ren

================================

Field 0 == tal.

Field 1 == 2 days

Field 2 == 751

================================

Field 0 == blank

================================

 

 

Any suggestions would be greatly appreciated.

 

Thanks.

Link to comment
Share on other sites

In the line

$fields = fgetcsv($fp, 1000, ",");

 

You specify the maximum line length in your source file as 1000 - The example line as posted by you is actually 994 characters long but I suspect it may be longer (either leading blanks or missing delimiters) - I'd suggest you  increase this number - once fgetcsv reaches the limit it treats further data as a new line.

 

 

EDIT: If you are running php 5 you don't need this parameter and can drop it

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.