1

So I'm attempting to explode my string of csv rows into an array and I'm having a difficult time.

Let me lead you through the steps:

var_dump($line); returns an string as this: :string 'TEST Test Test2 Test3

Now, when I have an .txt file, and I call $arr = explode("\t", $line);, the $arr returns an array

array (size=15)  
  0 => string 'TEST      ' (length=11)  
  1 => string 'Test' (length=4)  
  2 => string 'Test 2' (length=0)  
  3 => string 'Test 3' (length=6)  

But if I have an .csv file, and I attempt to explode it, I get left with an array string as such:

array (size=1) 0 => string 'TEST Test Test2 Test3'

I tried doing the following but I'm not having much luck: $test = str_getcsv($line, "\t", '', ''); - All the examples for str_getcsv show it like this:

$str = "the,cat,in,the,hat";
$arr = str_getcsv($str);
print_r($arr);

But mine aren't comma separated, but instead all tabbed.

All help would be appreciated.

1 Answers1

0

If you're using a file to get the csv's you should be able to use fgetcsv for this read more here.

$filename = "";
if (($handle = fopen($filename, "r")) !== false) {
    while (($line = fgetcsv($handle, 0, "\t")) !== false) {
        // $data should be an array of all the items in here.
        print_r($line);
    }
}
Spoon
  • 134
  • 1
  • 10
  • 1
    Apologies for the late follow-up, but this worked great @The Blacksmith! Would you be able to upvote my answer if it made sense :) –  Oct 15 '20 at 11:34