SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reading a CSV from TXT file

    I've got a text file with contents such as this:

    A, B, C
    D, E, F
    G, H, I

    I'm trying to figure out a way to read each line into a set of three variables that I can then manipulate.

    I found something like this:

    $splitcontents = explode($delimiter, $contents);

    But this is reading the file like so: A,B,C,D,E,F,G,H,I

    When I need to be able to read it as individual rows of three variables.

    As a background - this information is scraped from another website and fed into a txt file that is updated once a day. I did it this way to speed up page loads for my visitors.

    Can anyone help?

  2. #2

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Either use fgetcsv or load the file with file() function which will load it in array (each line will be one array) element, then go through the array and explode() each item to get CSV values.

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    I did find a bug within this function, that I didn't spend too much time researching.... But I believe depending on the format of the file, possibly even just the extension, fgetcsv will force load the entire file into memory before spooling.

    Not a huge deal except for when dealing with 3GB files like I usually am.

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,180
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by bvarvel View Post

    As a background - this information is scraped from another website and fed into a txt file that is updated once a day. I did it this way to speed up page loads for my visitors.
    I'm curious how this is helping performance?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •