Search code examples
phpparsingcsvdelimiterfgetcsv

Determine .csv delimiter in PHP


Note: I'll start off by saying that I know I'm probably missing something really obvious. I'm in one of those coding hazes where I can't see the simple solution.

Issue: I've written a script in PHP to parse a .csv file, select the column containing e-mail addresses, and put them into a database. Now, I've found that the users are attempting to upload files that have a .csv filetype, but are not actually comma-separated. I'm trying to write a function that will properly determine the delimiter (tab, new line, space, etc.), but am having some trouble with it. I think I would like to get an array of all of these addresses so that the number of keys would add credence to that delimiter.

The code:

$filename = "../some/path/test.csv";   
if (($handle = fopen($fileName, "r")) !== FALSE) {
    $delimiters = array(',', ' ', "\t", "\n");
    $delimNum = 0;
    foreach ($delimiters as $delimiter) {
      $row = 0;
      while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
        $data = (string)$data[0];
        $delimiterList[$delimNum] = explode($delimiter, $data);
        $row++;
    }
    $delimNum++;
}
die(print_r($delimiterList));
}

The result:

Array
(
[0] => Array
    (
        [0] => email
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
    )
)

Like I said, I know this is probably the wrong way to approach this, so I'm thankful for any insight you can provide!


Solution

  • Solve this problem with usability instead of code. Have the user pick the delimiter.

    However, since they may not know what tab delimited, CSV, et al mean, just show them a preview. They can pick from the options till the output looks correct and tabular.

    Then you parse it according to the format selected.