#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Explode File by Row


    Im stuck and hoping someone on this forum can assist.

    I have a file created in excel and then saved as tab-delimitted. The file has 6 columns and hundreds of rows. In column 1 is an identifier for each row, and in column 5 is the value I need to retrieve. I would like to open this file via php using file_get_contents, and then explode each line to break it down into actual rows (remember, this file is in txt form now and doesnt look like pretty excel). Once broken down by row, I need to loop through each row and check to the first column value to see if it meets a certain criteria, and if so return the value from column 5.

    I think this is possible but I cant figure it out. I cant really even come close. Can you share a small sample of code that would emulate what I need to do? Thanks in advance
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by fstephen07
    Im stuck and hoping someone on this forum can assist.

    I have a file created in excel and then saved as tab-delimitted. The file has 6 columns and hundreds of rows. In column 1 is an identifier for each row, and in column 5 is the value I need to retrieve. I would like to open this file via php using file_get_contents, and then explode each line to break it down into actual rows (remember, this file is in txt form now and doesnt look like pretty excel). Once broken down by row, I need to loop through each row and check to the first column value to see if it meets a certain criteria, and if so return the value from column 5.

    I think this is possible but I cant figure it out. I cant really even come close. Can you share a small sample of code that would emulate what I need to do? Thanks in advance
    could you give a few rows exactly as in your file? (or even better attach the file)
  4. #3
  5. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Forget file_get_contents() and explode() and look at fgetcsv instead.
  6. #4
  7. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by requinix
    Forget file_get_contents() and explode() and look at fgetcsv instead.
    Hey E-oreo, Small question: I assume you suggest to use fgetcsv() because with file_get_contents and explode you have to explode twice (per row and per tab) and use 2 arrays to sort it?


    At TS: this is my throw at it using the suggested fgetcsv hope it helps
    PHP Code:
    $row 1;
    if ((
    $handle fopen("tabbed1.txt""r")) !== FALSE) {
        while ((
    $data fgetcsv($handle1000"\t")) !== FALSE) {
            
    $num count($data);
            echo 
    "<p> $num fields in line $row: <br /></p>\n";
            
    $row++;
            for (
    $c 0$c $num$c++) {
                if(
    $c == 4){//  = the 5th value
                
    echo $data[$c] . "<br />\n";
                }
            }
        }
        
    fclose($handle);

  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Hey E-oreo, Small question: I assume you suggest to use fgetcsv() because with file_get_contents and explode you have to explode twice (per row and per tab) and use 2 arrays to sort it?
    The more useful feature of fgetcsv is that it handles enclosures and escape characters, which are sort of a pain to do manually. (Also that wasn't me you quoted originally)
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by E-Oreo
    (Also that wasn't me you quoted originally)
    Oh crap sorry the both of you, Thanks for responding anyway. I saw some nice colours in the avatar but was probably with my mind in another topic.

IMN logo majestic logo threadwatch logo seochat tools logo