0

*EDIT

I seem to be having some trouble with the fgetcsv function. In my code I am trying to create a single row array with each tab separated value as a separate element in the array. Instead of returning just the first value, $data[0] is returning the entire line. The problem seems to be that I don't have the right delimiter in fgetcsv, "/t" is not working. Any ideas how to fix this?

Here is my code,

#!/usr/bin/env php

<?php

require ("../includes/functions.php");

//argument argv[1] == the relative path 

 if (!file_exists($argv[1]))
 {
     print("The file you chose does not exist.");
 } 

 if(!is_readable($argv[1]))
 {

    print("The file you chose is not readable");
 }
 else
 {
     $handle=fopen($argv[1], "r");   

     $data = fgetcsv($handle, "\t");

     print($data[0]); 

}    

1 Answer 1

0

fgetcsv reads in the whole line by design: http://php.net/manual/en/function.fgetcsv.php

this is probably what you had in mind: http://php.net/explode

5
  • I am not sure I understand, what is the point of putting in the string delimiter \t in getcsv if it doesn't mark each element in the array. Commented May 14, 2015 at 9:16
  • It is clear to me now that getcsv will only ever read a single line. Thanks! I have re-edited my question specifically for the question of the delimiter. Commented May 14, 2015 at 10:15
  • incidentally, if you are doing pset 7, cs50 finance and not pset 8 as you have in your heading, then you can open the file as txt to see what the delimiter is
    – ronga
    Commented May 15, 2015 at 5:52
  • Ok so finally I took your advice and used explode and I got it to work! Still not sure why the other way is not working. Commented May 15, 2015 at 8:48
  • the other way works, but you need to know the correct delimiter as i indicated above. hint: what does csv stand for?
    – ronga
    Commented May 16, 2015 at 5:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .