Question

So, I have the following function using fgetcsv.

 public function CSVtoArray($value) {
      $row = 1;
      $file = "tmp/tmp.csv";
      file_put_contents($file, $value);
      if (($handle = fopen($file, "r")) !== FALSE) {
           $data = fgetcsv($handle, 1000, ",");
           fclose($handle);

           return $data;
      }
 }

The function takes csv input as such "something, something". The problem I've been having is that it keeps the space after the ,. I figured I could just add ", " too the delimiter, but it only takes one delimiter. I double check the documentation, and it'd seem that there isn't a workaround for that.

I know StackOverflow isn't a code gen service, but I'm stumped. Is there anyway too remove the spaces after the ,? Or is there a better way to do this?

Was it helpful?

Solution

Change:

return $data;

to:

return array_map('trim',$data);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top