0
    $file = new SplFileObject('D:\BackUp\addressbook.csv');
    print_r($file->getCsvControl());

What i am trying to do is find the delimiter of a csv file using php. the addressbook.csv file looks like

  "id";"firstname";"lastname";"phone";"email"
  "1";"jishan";"ishrak";"17878";"jishan.ishrak@gmail.com"

and another file is addressbook1.csv which is like

 "id","firstname","lastname","phone","email"
 "1","jishan","ishrak","17878","jishan.ishrak@gmail.com"

one is separated by "," and another one is with ";" but the function

  getCsvControl()

always returns an array like

 Array ( [0] => , [1] => " )

I mean in the [0] index it always gives "," for both files is there a way to solve this issue.

jishan
  • 300
  • 1
  • 4
  • 20

2 Answers2

0

Proabably this is a bug? as you can see here php doc 1st comment 1 year ago - Seems that this function always returns the same delimiter.

UPDATE this is not a bug look at Pazi ツ answer.

  • @jishan - this might help finding delimiter - http://stackoverflow.com/questions/3395267/how-to-find-out-if-csv-file-fields-are-tab-delimited-or-comma-delimited – Peter Popelyshko Jan 18 '15 at 12:14
0

This is not a bug. SplFileObject::getCsvControl() is never intended to detect the delimiter from a CSV file. It returns only the default control characters or the one previously set with SplFileObject::setCsvControl(). And this set CSV control characters are used, if is nothing handed over in the SplFileObject::fgetcsv() method.

Ok, it's badly documented, but this were my first thoughts, the method would never detect the characters and a look into the php source code confirmed this.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
  • in that case how can I find the delimiter from a csv file... @pazi – jishan Jan 19 '15 at 03:35
  • Look at the comment Peter gave in the other answer: "@jishan - this might help finding delimiter - http://stackoverflow.com/questions/3395267/how-to-find-out-if-csv-file-fields-are-tab-delimited-or-comma-delimited " – Emii Khaos Jan 19 '15 at 05:08